loading two components in angular2 non SPA

后端 未结 2 1660
你的背包
你的背包 2021-01-23 13:19

Given the following components. I want to use these components on a non SPA web site exactly like the plunker here. This sample is no longer valid, as the latest beta of angular

相关标签:
2条回答
  • 2021-01-23 13:27

    The answer to anyone else attempting this, is to create two modules, and register each module separately

    Registration

    //main entry point
    import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
    import {AppModuleA, AppModuleB} from './app';
    
    platformBrowserDynamic().bootstrapModule(AppModuleA);
    platformBrowserDynamic().bootstrapModule(AppModuleB);
    

    Modules

    @NgModule({
      imports: [ BrowserModule ],
      declarations: [ AppA ],
      bootstrap: [ AppA ]
    })
    export class AppModuleA {}
    
    @NgModule({
      imports: [ BrowserModule ],
      declarations: [ AppB ],
      bootstrap: [ AppB ]
    })
    export class AppModuleB {}
    
    0 讨论(0)
  • 2021-01-23 13:44

    Seems you are missing a module definition e.g.:

    app.module.ts

    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    
    import {Comp1} from './comp1';
    import {Comp2} from './comp2';
    
    @NgModule({
        imports: [
            BrowserModule
        ],
        declarations: [
            Comp1,
            Comp2
        ],
        bootstrap: [Comp1]
    })
    
    export class AppModule { }
    

    main.ts

    import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
    
    import { AppModule } from './app.module';
    
    platformBrowserDynamic().bootstrapModule(AppModule);
    
    0 讨论(0)
提交回复
热议问题