首页 > 解决方案 > 如何在不使用多行代码的情况下在视图中显示或不显示组件

问题描述

我看过很多例子,但我仍然相信可以有一种更简单的方法,而无需在创建的每个组件中包含代码。我的想法不是在我的登录中显示 2 个组件,而是在我的其余组件中显示,是的。

app.component.html

<menu></menu>
<router-outlet></router-outlet>
<footer_component></footer_component>

标签: angular

解决方案


如果您不想渲染 app.component.html 中的所有组件,可以使用带条件的 *ngIf 指令

<menu *ngIf="condition"></menu>
<router-outlet></router-outlet>
<footer_component *ngIf="condition"></footer_component>

推荐阅读