簡體   English   中英

啟用 Ivy 的 Angular 庫無法加載

[英]Angular Library with Ivy enabled fails to load

我正在嘗試創建一個角度庫項目,但在控制台中出現此錯誤

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)

我用ng new --enable-ivy創建了一個新的 angular 項目,然后用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
  1. npm uninstall -g @angular/cli
  2. npm install -g @angular/cli@8.3.19 --check your project CLI version 3.ng_Modules 從本地文件夾中刪除
  3. 安裝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM