angular - 如何从库中导入组件(使用 Angular 在 nrwl/nx 中)

标签 angular typescript nrwl nrwl-nx

我想导入我的 PageNotFoundComponent来自我的 ui-components库到我的应用程序的路由器中。

当我导入 UiComponentsModule进入我的 AppModule并使用我模板中的组件,一切正常,但以 es6 表示法导入组件失败。

/libs/ui-components/src/lib/ui-components.module.ts

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { ContactCardComponent } from './contact-card/contact-card.component';
import { NotFoundComponent } from './not-found/not-found.component';
import { WhiteLabelFooterComponent } from './white-label-footer/white-label-footer.component';
import { WhiteLabelHeaderComponent } from './white-label-header/white-label-header.component';

@NgModule({
  declarations: [
    ContactCardComponent,
    WhiteLabelFooterComponent,
    WhiteLabelHeaderComponent,
    NotFoundComponent
  ],
  exports: [
    ContactCardComponent,
    WhiteLabelFooterComponent,
    WhiteLabelHeaderComponent,
    NotFoundComponent
  ],
  imports: [CommonModule],
})
export class UiComponentsModule {}

/apps/myApp/src/app/app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { NotFoundComponent } from 'libs/ui-components/src/lib/not-found/not-found.component';

const routes: Routes = [
  {
    path: '**',
    component: NotFoundComponent,
  },
];

@NgModule({
  exports: [RouterModule],
  imports: [RouterModule.forRoot(routes)],
})
export class AppRoutingModule {}

使用类似 import { NotFoundComponent } from 'libs/ui-components/src/lib/not-found/not-found.component'; 的导入我收到短绒错误:

library imports must start with @frontend/ (nx-enforce-module-boundaries)tslint(1) Submodule import paths from this package are disallowed; import from the root instead (no-submodule-imports)tslint(1) Module 'libs' is not listed as dependency in package.json (no-implicit-dependencies)tslint(1)



当我将导入更改为 import { NotFoundComponent } from '@frontend/ui-components'; 时然后我得到错误:

Module '"../../../../../../../../../Users/LeitgebF/Projects/KLAITONWeb/frontend/libs/ui-components/src"' has no exported member 'NotFoundComponent'.ts(2305)



那么如何直接从 Nx 中的库中导入组件呢?

最佳答案

我也必须将所需的组件添加到我的桶文件中。这是直接来自 github repo 支持的答案:https://github.com/nrwl/nx/issues/1533

我不明白,为什么我需要 Angular 模块,但我创建了它并导出了桶文件中的其他组件。

关于angular - 如何从库中导入组件(使用 Angular 在 nrwl/nx 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56806124/

相关文章:

javascript - Angular 5在每次点击路线时滚动到顶部

angular - 关于 rxjs observables 的基本问题——如何将数据提供给 observable 'by hand' ?

css - 在 Angular 2/Ionic 2 中给定条件下右对齐列

unit-testing - 如何在karma中模拟模拟服务失败?

javascript - React + Typescript 继承和扩展属性

angular - 在 Angular 2、Ionic 2 中返回 promise 值

angular - 如何在 NX 工作区中运行特定的 Angular 规范测试文件?

Angular MonoRepo Nx - 在应用程序中使用库

Angular2 material 2 Autocomplete如何改变下拉的宽度?