angular - 共享的角度模块未登陆 webpack 公共块
问题描述
我有很多在整个应用程序中重用的角度模块,例如
- I18n 模块
- 字符串输入模块
- ...
在通过 Angular CLI 构建应用程序并使用 webpack 包分析器检查包后,这些共享模块会在包含它们的不同模块中弹出。有没有办法强迫它们进入common
块?或者有哪些可能的错误会导致这种代码重复行为,而不是检测到它确实是一个共享模块。
澄清一下:公共块存在,但实际上只有少数约 50 个共享模块捆绑在该块中。其余的要么在不同的功能块中重复,要么很多都在第一个功能页面模块中。
AoT 已启用,commonChunk
选项也已启用(https://angular.io/cli/build)
解决方案
您可以在所有可重用组件所在的位置创建一个 sharedModule,然后在需要时将该模块导入 import sharedModule 的 appmodule 中:
$ng gm 共享--路由
$ ng gc shared/components/shared --module 共享
shared.module.ts
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { SharedRoutingModule } from "./shared-routing.module";
import { SharedComponent } from "./components/shared/shared.component";
@NgModule({
declarations: [SharedComponent],
imports: [CommonModule, SharedRoutingModule],
exports: [SharedComponent]
})
export class SharedModule {}
app.module.ts
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { SharedModule } from "./shared/shared.module";
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, SharedModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
first.module.ts
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { ExampleRoutingModule } from "./example-routing.module";
import { ExampleComponent } from "./components/example/example.component";
import { SharedModule } from "../shared/shared.module";
@NgModule({
declarations: [ExampleComponent],
imports: [CommonModule, ExampleRoutingModule, SharedModule]
})
export class ExampleModule {}
推荐阅读
- triggers - 触发器测试类的测试覆盖率不断显示 53
- php - PHP 7.x SQLITE3 PDO - execute() 是否关闭了 PDO 连接?
- submenu - 我们可以在 react-admin 中为用户、帖子和仪表板添加子菜单吗?如果可以,我们如何添加?
- linux - top 命令在linux中是如何工作的?它如何提供实时输出?
- amazon-web-services - 如何在同一 AWS EB 应用程序中托管基于不同版本的路由
- elasticsearch - ElasticSearch 日期范围查询不适用于自定义日期格式
- javascript - 从我的 React 组件中的文本中打印字母,就像旧的视频游戏一样
- ios - 无法添加钥匙串项。错误 - 使用 KeychainItemWrapper 更改标识符后出现 25299?
- javascript - 我们可以同时使用 `export default` 和 `module.exports` 吗?
- angular - 为不同的 REST / WebServices 配置不同的 Angular 6 HttpClient 标头