【问题标题】:Angular can't find module when build (prod only)Angular 在构建时找不到模块(仅限产品)
【发布时间】:2020-01-21 17:38:47
【问题描述】:

我正在使用 Angular cli 构建一个 Angular 模块。在另一个项目中,我调用了这个模块。

当我运行ng serve 时,一切正常。但是当我运行ng build --prod 时,出现以下错误:

错误 ./node_modules/my-module/dist/pagination/pagination.ngfactory.js 找不到模块:错误:无法解析“分页” '/my-web-application/node_modules/my-module/dist/pagination'

./src/app/app.module.ngfactory.js 中的错误模块未找到:错误: 无法解析“/my-web-application/src/app”中的“分页”

./src/app/areas/private/users/list/index.ngfactory.js 模块中的错误 未找到:错误:无法解析“分页” '/my-web-application/src/app/areas/private/users/list'

在我的网络应用程序中,我以这种方式加载我的模块

@NgModule({
  ...
  imports: [
    MyModule
  ],
  ...
  bootstrap: [AppRootComponent]
})
export class AppModule { }

并以这种方式调用组件模块

<pagination [data]="pagination"></pagination>

在 MyModule 中,它是这样配置的

@NgModule({
  declarations: [PaginationComponent],
  imports: [
    CommonModule,
  ],
  exports: [PaginationComponent]
})
export class MyModule { }

我该怎么做才能使它仅在 ng build --prod 时崩溃?

Angular CLI: 8.3.5
Node: 10.15.0
OS: darwin x64
Angular: 8.2.7
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.803.5
@angular-devkit/build-angular     0.803.5
@angular-devkit/build-optimizer   0.803.5
@angular-devkit/build-webpack     0.803.5
@angular-devkit/core              8.3.5
@angular-devkit/schematics        8.3.5
@angular/cdk                      8.2.0
@angular/cli                      8.3.5
@angular/flex-layout              8.0.0-beta.27
@angular/material                 8.2.0
@ngtools/webpack                  8.3.5
@schematics/angular               8.3.5
@schematics/update                0.803.5
rxjs                              6.4.0
typescript                        3.5.3
webpack                           4.39.2

https://github.com/angular/angular-cli/issues/15645

【问题讨论】:

    标签: angular angular-cli


    【解决方案1】:

    在 MyModule 中将 PaginationComponent 添加到导出数组中:

    @NgModule({
      exports: [
        PaginationComponent
      ]
    })
    export class MyModule {}
    

    编辑:

    也用于:

    <pagination [data]="pagination"></pagination>
    

    确保pagination 属性在您实现该代码的方法中是公开的

    【讨论】:

    • 在 PaginationComponent 内部,我在构造函数中有一些私有属性。我将所有设置为公开,但错误仍然存​​在。
    【解决方案2】:

    您还需要在 AppModule 中导出 MyModule 模块,如下所示:

    @NgModule({
      ...
      imports: [
        MyModule
      ],
      exports: [MyModule]
      ...
      bootstrap: [AppRootComponent]
    })
    export class AppModule { }
    

    【讨论】:

    • ERROR in module 'AppModule' is exported recursively by the module 'AppModule
    • @Pablo 抱歉我解决了这个问题,检查更新的代码
    猜你喜欢
    • 1970-01-01
    • 2019-01-14
    • 2022-11-08
    • 2017-10-24
    • 2022-10-26
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多