Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

angular - Dynamically add components & module in NgModule declaration & imports

I have key value pair of component classes, which i used to generate routing dynamically. example -

export const NODE_COMPONENT = {
  climate: ClimatePageComponent,
  project: ProjectDataPageComponent,
}

export const NODE_MODULE = {
  climate: ClimateModule,
  project: ProjectModule,
}

Now I want to use these key value pair to generate declaration, entryComponent & imports using below code -

@NgModule({
  declaration: [ otherComponents ],
  import: [otherModules],
})
export class PageModule {
  constructor(private module: NgModule) {
    this.loadImports();
  }

  loadImports() {
    for (const key in NODE_COMPONENT) {
        this.module.declarations.push(NODE_COMPONENT[key]);
        this.module.entryComponents.push(NODE_COMPONENT[key]);
        this.module.imports.push(NODE_MODULE[key]);
    }
  }
}

but it's throwing compilation error Cannot determine the module for class ClimatePageComponent Add ClimatePageComponent to the NgModule to fix it

Let me know If it's possible or do i need to import it manually?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...