4

我正在尝试创建一个角度库项目,但我在控制台中收到此错误

Uncaught TypeError: Cannot read property 'id' of undefined
    at registerNgModuleType (core.js:34470)
    at core.js:34488
    at Array.forEach (<anonymous>)
    at registerNgModuleType (core.js:34484)
    at new NgModuleFactory$1 (core.js:34651)
    at compileNgModuleFactory__POST_R3__ (core.js:40286)
    at PlatformRef.bootstrapModule (core.js:40609)
    at Module../src/main.ts (main.ts:11)
    at __webpack_require__ (bootstrap:79)
    at Object.0 (main.ts:12)

我创建了一个新的 Angular 项目,ng new --enable-ivy然后添加了一个带有ng g library my-lib. 我的图书馆有一个带有模块(如下)的辅助端点,因此导入将是import { MyLibModule } from 'my-lib/secondary';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    MyLibDirective
  ],
  exports: [
    MyLibDirective
  ]
})
export class MyLibModule {
  public static forRoot(): ModuleWithProviders {
    return {
      ngModule: MyLibModule,
      providers: []
    };
  }
}

我使用ng build my-lib它构建它,所有构建都很好,我使用链接将库包含在演示应用程序中,在我运行的库的 dist 文件夹和我运行yarn link的根演示应用程序文件夹中yarn link my-lib,然后在导入中包含模块这AppModule

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MyLibModule } from 'my-lib/secondary';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    MyLibModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

但是,当我运行yarn startng serve一切正常但我在控制台中收到上述错误时,在浏览器中调试它会显示ngModuleDef我的库模块中的属性不存在,有谁知道为什么会发生这种情况?

注意:如果我关闭 Ivy,这不是问题,但随着 ng9 即将推出,这不是一个真正可行的解决方案,我想知道我在库中做错了什么(如果有的话)

输出自ng version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 8.3.21
Node: 12.12.0
OS: darwin x64
Angular: 8.2.14
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.803.21
@angular-devkit/build-angular      0.803.21
@angular-devkit/build-ng-packagr   0.803.21
@angular-devkit/build-optimizer    0.803.21
@angular-devkit/build-webpack      0.803.21
@angular-devkit/core               8.3.21
@angular-devkit/schematics         8.3.21
@angular/cli                       8.3.21
@ngtools/webpack                   8.3.21
@schematics/angular                8.3.21
@schematics/update                 0.803.21
ng-packagr                         5.7.1
rxjs                               6.5.4
typescript                         3.5.3
webpack                            4.39.2
4

1 回答 1

0
  1. npm 卸载 -g @angular/cli
  2. npm install -g @angular/cli@8.3.19 --检查您的项目 CLI 版本 3.ng_Modules 从本地文件夹中删除
  3. npm 安装
于 2021-05-22T17:45:04.023 回答