如何从库中导入组件(在 nrwl/nx 和 Angular 中)
How to import a component from a library (in nrwl/nx with Angular)
我想将我的 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';
这样的导入,我得到了 linter 错误:
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 模块,但我创建了它并导出了桶文件中的其他组件。
我想将我的 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';
这样的导入,我得到了 linter 错误:
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 模块,但我创建了它并导出了桶文件中的其他组件。