假设 client-2 是餐厅名
ng g module dynamic-modules/client-2 --routing
创造新的客户模块ng g component dynamic-modules/client-2/shell
在新的客户模块下创造shell组件ng g component dynamic-modules/client-2/home
在新的客户模块下创造home组件- 在新创造的客户模块文件夹内找到routing文件(client2-routing.module.ts),把
forChild
改成forRoot
- 在routing文件内define routes,eg.
export const enum ClientRoutes { home = "home", } const routes: Routes = [ { path: ClientRoutes.home, component: HomeComponent }, { path: "*", redirectTo: ClientRoutes.home }, { path: "**", redirectTo: ClientRoutes.home } ];
- 在新创造的客户模块文件夹下找到module文件(client-2.module.ts),在
import
里添加DynamicComponentLoaderModule.forChild(ShellComponent),
记得重新打Dynamic以自动importDynamicComponentLoaderModule
- 新的客户模块下找到
shell.component.ts
, 把class里换成如下
记得importconstructor(private router: Router) { } ngOnInit() { this.router.navigate([`${ClientRoutes.home}`]); }
- 在
shell.component.html
换成<router-outlet></router-outlet>
- 在
app.module.ts
里找到manifests
数组,并在最后添加 eg:{ componentId: 'client-2', path: 'client2-shell', loadChildren: './dynamic-modules/client-2/client2.module#Client2Module' }
- 在
app.component.ts
里手动添加shell的import, eg:import { ShellComponent as client2Shell } from './dynamic-modules/client-2/shell/shell.component';
- 在
app.component.ts
里关于host的逻辑,在对应的block里添加factory$ = this.dynamicComponentLoader.getComponentFactory<client2Shell>( 'client-2' );