基于url的引导组件
我也有同样的问题,我的解决方案是根据url引导不同的组件,而不是引导不同的模块
解决方案基于本文:
How to manually bootstrap an Angular application
你不能编辑主.ts但是在AppModule中,您可以根据url手动引导不同的组件并将它们添加到DOM中
应用程序模块.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ContactplanDetailsComponent } from './ContactplanDetails.component';
import { ContactplanListComponent } from './ContactplanList.component';
import { MenuComponent } from './Menu.component';
@NgModule({
imports: [BrowserModule],
declarations: [
ContactplanDetailsComponent,
ContactplanListComponent,
MenuComponent
],
//bootstrap: [AppComponent] Bootstrapping is done manually
entryComponents: [
ContactplanDetailsComponent,
ContactplanListComponent,
MenuComponent
]
})
export class AppModule {
ngDoBootStrap(app) {
bootstrapComponent(app);
}
}
function bootstrapComponent(app) {
var name = [];
const options = {
'contact-plan-details': ContactplanDetailsComponent,
'contact-plan-list': ContactplanListComponent,
'menu': MenuComponent
};
if (window.location.pathname.toLowerCase().includes("contactplans/details")) {
name.push("contact-plan-details");
} else if (window.location.pathname.toLowerCase().includes("contactplans/index") || window.location.pathname.toLowerCase().endsWith("contactplans") || window.location.pathname.toLowerCase().includes("settings/contactplans")) {
name.push("contact-plan-list");
}
name.push("menu")
name.forEach(function (element) {
const componentElement = document.createElement(element);
document.body.appendChild(componentElement);
const component = options[element];
app.bootstrap(component);
});
}