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

2024-01-30

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{

}

与 HTML 1.html 和 HTML 2.html 一起

HTML1.html

<h1>view1</h1>

HTML2.html

<h1>view2</h1>

我可以在 angular2 中使用多个 templateUrl 吗? 感谢您花时间阅读本文:)


您不能添加多个 HTML 文件。我也不明白这会达到什么目的。

您可以使用*ngIf or *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';
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 angular2 的一个组件中添加多个 templateUrl 的相关文章

随机推荐