我正在尝试在同一页面中引导两个angular2应用程序。我尝试使用platformBrowserDynamic.bootstrapModule()来做到这一点。但是它让我误以为“试图找到引导代码,但不能。指定静态可分析的引导代码或将entryModule传递给插件选项。”
//Main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { BrowserModule } from '@angular/platform-browser';
import { AppModule1 } from './app1/app.module1';
import { AppModule2 } from './app2/app.module2';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule1 );
platformBrowserDynamic().bootstrapModule(AppModule2 );
所以我尝试通过AppModule1引导AppModule2,如下所示,这有效。
//app1.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppService } from './app.service';
import { App1RoutingModule } from '../app1/app1-routing.module';
import { App1Component } from '../app1/app1.component';
import { App2Module } from '../app2/app2.module';
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
App1RoutingModule,
App2Module
],
declarations: [App1Component],
providers: [AppService],
bootstrap: [App1Component ]
})
export class App1Module { }
platformBrowserDynamic().bootstrapModule(AppModule2);
虽然上面的解决方案有效,但我可以创建构建,但是当部署在Tomcat上时,它无法正常工作。
如何在同一个屏幕中引导2个应用程序?
答案 0 :(得分:3)
您可以在同一页面上引导两个不同的模块。你的第一次尝试应该有效。这是plnkr,在同一页面上引导了两个模块。
app.ts:
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
</div>
`,
})
export class App {
name:string;
constructor() {
this.name = `Angular! v${VERSION.full}`
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
@Component({
selector: 'my-app2',
template: `
<div>
<h2>Hello2 {{name}}</h2>
</div>
`,
})
export class App2 {
name:string;
constructor() {
this.name = `Angular! v${VERSION.full}`
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App2 ],
bootstrap: [ App2 ]
})
export class AppModule2 {}
main.ts:
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule, AppModule2 } from './app';
platformBrowserDynamic().bootstrapModule(AppModule)
platformBrowserDynamic().bootstrapModule(AppModule2)