在angular2中的一个组件中添加多个templateUrl

前端之家收集整理的这篇文章主要介绍了在angular2中的一个组件中添加多个templateUrl前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
angular2允许使用反向标记(`)编写多行HTML代码.
 但是当使用templateUrl时,我不知道如何添加多个html文件.

当我试试这个..

@Component({
  selector: 'my-app',template: `
           <h1>view1</h1>
           <h1>view2</h2>
            `
})

class Appcomponent{

}

像那样.

@Component({
  selector: 'my-app',templateUrl: './HTML1.html','./HTML2.html'


})

class Appcomponent{

}

与HTML1.html和HTML2.html一起使用

HTML1.html

<h1>view1</h1>

HTML2.html

<h1>view2</h1>

我可以在angular2中使用多个templateUrl吗?
谢谢你的时间阅读这个:)

解决方法

您不能添加多个HTML文件.我也看不出这会达到什么目的.

如果您的意图是,您可以使用* ngIf或* ngSwitchCase仅显示模板的部分内容

@Component({
  selector: 'my-app',template: `
           <h1 *ngIf="view == 'view1'>view1</h1>
           <h1 *ngIf="view == 'view2'>view2</h2>
           <button (click)="view = view == 'view1' ? 'view2' : 'view1'">toggle</button>
            `
})

class Appcomponent{
  view = 'view2';
}

猜你在找的Angularjs相关文章