gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:59:30 25 4
gpt4 key购买 nike

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';
}

关于angular - 在angular2的一个组件中添加多个templateUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40880254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com