Search Docs
本节我们将详细介绍 angular 框架的应用作为子应用的接入步骤。demo
# 1. 安装 @angular-builders/custom-webpack:browser npm install @angular-builders/custom-webpack:browser -D # 2. 安装 @angular-builders/custom-webpack:dev-server npm install @angular-builders/custom-webpack:dev-server -D
// angular.json "builder": "@angular-builders/custom-webpack:browser",
// angular.json "options": { "customWebpackConfig": { // 新增 webpack 配置 "path": "./custom-webpack.config.js" }, "index": "", }
// angular.json "builder": "@angular-builders/custom-webpack:dev-server",
【重要】注意:
// custom-webpack.config.js const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { output: { filename: '[name].[contenthash].js', chunkFilename: '[name].[contenthash].js', libraryTarget: 'umd', globalObject: 'window', chunkLoadingGlobal: 'Garfish-demo-angular', publicPath: 'http://localhost:8080' }, plugins: [ new HtmlWebpackPlugin({ filename: 'index.html', template: path.join(__dirname, 'src/index.html'), chunksSortMode: 'manual', chunks: ['styles', 'runtime', 'polyfills', 'scripts', 'vendors', 'main'], scriptLoading: 'defer', }), ], devServer: { headers: { 'Access-Control-Allow-Origin': '*', }, }, };
"scripts": { "builder": "@angular-builders/custom-webpack:dev-server" }
// src/main.ts import { enableProdMode, NgModuleRef } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; import { environment } from './environments/environment'; if (environment.production) { enableProdMode(); } let app: void | NgModuleRef<AppModule>; async function render() { await platformBrowserDynamic() .bootstrapModule(AppModule) .catch((err) => console.error(err)); } export const provider = ({ dom, basename, props}) => { return { render, destroy({ dom }) { const root = dom ? dom.querySelector('#root') : document.querySelector('#root'); }, }; };
// app.module.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { RouterModule } from '@angular/router'; import { ReactiveFormsModule } from '@angular/forms'; import { AppComponent } from './app.component'; import { TopBarComponent } from './topBar/topBar.component'; import { HomeComponent } from './home/home.component'; import { APP_BASE_HREF } from '@angular/common'; @NgModule({ imports: [ BrowserModule, ReactiveFormsModule, RouterModule.forRoot([ { path: '/home', component: HomeComponent } ]) ], providers: [{ provide: APP_BASE_HREF, useValue: '/examples/angular' }], declarations: [ AppComponent, TopBarComponent, ], bootstrap: [AppComponent], }) export class AppModule {}
last but not least, 别忘了添加子应用独立运行逻辑,这能够让你的子应用脱离主应用独立运行,便于后续开发和部署。
// src/main.ts import { enableProdMode, NgModuleRef } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; async function render() { await platformBrowserDynamic() .bootstrapModule(AppModule) .catch((err) => console.error(err)); } if (!(window as any).__GARFISH__) { render(); }