我将使用 Angular 7 框架开发一个非常大的应用程序。
我使用创建了一个空白的角度工作区
ng new angular-app --create-application=false
在这个工作区中,我使用以下命令创建了两个 Angular 应用程序:
ng generate application app-one
ng generate application app-two
在这两个应用程序中,我将有多个组件,每个组件都彼此独立工作。
我正在寻找一种方法来为每个组件创建一个单独的 javascript 构建文件,以减少构建大小。
并使用每个单独构建的 js 文件将每个组件用作 Web 组件。
请阅读我已经尝试过的内容以获得更好的想法。
我尝试了以下步骤:
为自定义角度元素创建一个带有 custom 前缀的存储库:
ng new app-name --prefix custom
添加角度元素包:
ng 添加@angular/元素
根据需要创建封装为 native/emulated/none 的自定义元素组件:
ng g 组件 my-component --inline-style --inline-template -v Native
在 app.modulte.ts 中定义自定义元素
import { Injector} from '@angular/core';
import { createCustomElement } from '@angular/elements';
...
export class AppModule {
constructor(private injector : Injector){
const el = createCustomElement(MyComponent, {injector : this.injector});
customElements.define('my-component',el);
}
ngDoBootstrap(){ }
}
安装 ngx-build-plus 包以构建单个包(例如,用于 Angular Elements):
npm i ngx-build-plus
在 angular.json 文件中更新应用程序的 builder 部分,使其指向 ngx-build-plus:
"builder": "ngx-build-plus:build",
在 package.json 中添加脚本以运行构建器:
"build:ngx": "ng build --prod --output-hashing none --single-bundle true"
如果需要,通过创建 js 文件“concat_ngx.js”将创建的 dist 文件夹中的 scripts.js 和 main.js 合并:
const fs = require('fs-extra');
const concat = require('concat');
(async function build() {
const files = [
'./dist/<your_project>/scripts.js',
'./dist/<your_project>/main.js',
]
await fs.ensureDir('elements_ngx')
await concat(files, 'elements_ngx/combined-script.js');
})()
运行文件以获取单个 js 文件:
节点 concat_ngx.js
在任何 Angular/Other 项目中使用 js 文件来使用创建的自定义组件。
但这里的问题是我每次都必须在 app-module.ts 中更改组件引导程序
我需要一种自动化的方式来在运行时更改 app-module.ts 中的引导。
但这里的问题是我每次都必须在 app-module.ts 中更改组件引导程序
我需要一种自动化的方式来在运行时更改 app-module.ts 中的引导。