在延迟加载的模块中进行引导

时间:2019-03-07 11:09:13

标签: angular

Angular Guide官员说: “ 除其他外,引导过程会创建引导数组中列出的组件,并将每个组件插入浏览器DOM。

但是在我看来,我无法在延迟加载的功能模块中引导组件。当我路由特定的URL时,将加载我的功能模块(调用了构造函数),但是bootstrap: [MyComponent]中定义的组件既不呈现,也不构造函数或ngOnInit函数被调用。我在做错什么吗?


app-routing.module.ts

const routes: Routes = [
      { path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule'}

dashboard.module.ts

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';    
import { Dashboard } from './dashboard.component';


@NgModule({
  imports: [
 CommonModule
    DashboardRoutingModule
  ],
  declarations: [DashboardComponent],
  bootstrap: [DashboardComponent]
})
export class DashboardModule {
  constructor() {
    console.log("DashboardModule loaded");
  }
}

dashboard.component.ts

import {Component, OnInit} from '@angular/core';

@Component({
  selector: 'app-test',
  styles: [],
  template: `
    foo bar
  `,
})
export class TestComponent implements OnInit {
  constructor() {
    console.log("TestComponent construct");
  }
  ngOnInit() {
    console.log("TestComponent init");
  }
}

2 个答案:

答案 0 :(得分:1)

使用entryComponents数组实现相同的功能。 更多信息是here

        <dependency>
          <groupId>org.eclipse.swt</groupId>
          <artifactId>org.eclipse.swt</artifactId>
          <scope>compile</scope>
        </dependency>

答案 1 :(得分:-2)

TestComponent

  

dashboard.component.ts