Can one component have multiple templates?

后端 未结 2 1723
醉梦人生
醉梦人生 2021-01-06 13:36

Is there a way for Angular 2 component to use many template files base on where i want to place it?

For example, I have a login component and i want to

相关标签:
2条回答
  • 2021-01-06 14:26

    Not sure if NG2 has built-in way to support this. I just used a base component class to contain all or most logic and data, without a html template. Then in derived component classes, just need to declare constructor calling super(...), and define respective html template.

    If your application is complex, likely you will be using module to instantiate classes, then make sure you declare moduleId: module.id in the Component attribute, otherwise the NG2 runtime may complain the html template could not be loaded.

    0 讨论(0)
  • 2021-01-06 14:33

    The simplest way to do this, i think, is using *ngIf in your template.

    <div *ngIf="template === 1"> First Template </div>
    <div *ngIf="template === 2"> Second Template </div>
    
    0 讨论(0)
提交回复
热议问题