Dynamically add Component in angular 2/4

前端 未结 3 2017
日久生厌
日久生厌 2021-01-07 03:29

How can I add component dynamically?

toolbar.component.ts:

@Component({
  selector: \'app-toolbar\',
  template: \'
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-07 04:22

    I have another way of doing it, please check for you case

    html
    
    
    .ts
    import { COMPONENT } from './..dir../component'
    

    OR

    html
    
    
    .ts
    import { COMPONENT } from './..dir../component'    
    option = {
      component: COMPONENT
    }
    

    OR if it is Dynamic, add the component to entryComponent

    app.module.ts
    import { COMPONENT } from './..dir../component'
    @NgModule({
       declarations: [COMPONENT],
       /.
       ..
       ..
      ./
      entryComponents: [COMPONENT]
    });    
    
    html
    
    
    .ts
    /** The COMPONENT is not imported since it is added to entrycomponent**/
    @Input() option;
    

提交回复
热议问题