如何从自定义Angular库导入特征模块?

如何从自定义Angular库导入特征模块?

问题描述:

类似于此问题使用多个功能模块构建了一个自定义Angular库(使用 Angular CLI 7.2.0 ):

Similar to this question I built a custom Angular library (with Angular CLI 7.2.0) with multiple feature modules:

MyLibraryModule
  FeatureModule1
    SomeComponent1
  FeatureModule2
    SomeComponent2

我只想将一个功能模块导入我的主应用程序,就像我们可以对Angular Material模块进行的操作一样( import {MatButtonModule} from'@ angular/material'; ):

I want to import only one feature module into my main application, like we can do with Angular Material modules (import { MatButtonModule } from '@angular/material';):

import { FeatureModule1 } from 'my-library';
...
@NgModule({
  imports: [
    ...,
    FeatureModule1,
  ]
})
...

这会导致以下错误(在构建主项目期间):

This results in the following error (during building the main project):

ERROR in : Unexpected value 'FeatureModule1 in ./node_modules/my-library/my-library.d.ts'
imported by the module 'AppModule in ./src/app/app.module.ts'.
Please add a @NgModule annotation.

当我将库模块(从'my-library'; 导入 import {MyLibraryModule})导入主应用程序时,一切正常.

When I import the library module (import { MyLibraryModule } from 'my-library';) into my main application, everything works.

my-library.module.ts 看起来像这样:

import { NgModule } from '@angular/core';

import { FeatureModule1 } from './feature-module-1/feature-module-1.module';
import { FeatureModule2 } from './feature-module-2/feature-module-2.module';

@NgModule({
  imports: [ FeatureModule1, FeatureModule2 ],
  exports: [ FeatureModule1, FeatureModule2 ],
})
export class MyLibraryModule{ }

我的 public_api.ts 导出所有模块.

我认为这与打包过程有关,因为在我的测试项目(与库位于同一文件夹中)中,该工作没有任何问题.

I think it has something to do with packaging process, because in my test project (which is in the same folder as the library) this works without any problems.

我已经尝试适应构建过程来自 @ angular/material ,但看来,他们的做法与 official 方式有所不同.

I already tried to adapt the building process from @angular/material but it seems, they are doing it a bit different then the official way.

问题是,我使用了 public_api.ts 中导入的功能模块( index.ts )中的="nofollow noreferrer">桶文件:

The problem was, that I used barrel files in my feature modules (index.ts) that I imported in my public_api.ts like this:

import * from './feature-module-1';

但是如此处所述,您必须直接指向桶文件.否则,软件包的 .d.ts 文件不会包含所有导出.因此,我的 public_api.ts 中的以下行解决了该问题:

But as described here, you have to point directly at the barrel file. Otherwise the .d.ts file for the package does not contain all exports. So the following line in my public_api.ts fixed the problem:

import * from './feature-module-1/index';