Angular - 周围的条件 div 容器

2024-03-24

在我的 app.component.html 中,我想根据当前 url 渲染某些 div 容器。例如

1. 如果当前 URL 是authenticate,则呈现以下内容

<div *ngIf="'authenticate' === this.currentUrl">
    <router-outlet></router-outlet>
</div>

2. 如果当前 URL 未通过身份验证,则呈现以下内容

<div *ngIf="'authenticate' !== this.currentUrl" id="wrapper">
    <div id="page-content-wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-12">
                    <div class="row extranet-outlet-wrapper">
                        <router-outlet></router-outlet>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

我没有使用任何子路线。这是因为,我只想为身份验证组件使用不同的布局,其余部分保持不变。

这在第一次加载时有效,但是当我单击任何[routerLink]视图不会更新。但是,如果我重新加载屏幕,它就会起作用。问题在于条件渲染<router-outlet> in app.component.html,我通过删除条件来检查这一点,它工作得很好。

有人可以帮助我了解这里发生的情况以及如果可能的话如何在不使用子路由的情况下解决这个问题。


您可以尝试这个解决方案。

<ng-container
  *ngIf="'authenticate' !== this.currentUrl; then notauthenticated; else authenticate">
</ng-container>

<ng-template #notauthenticated>
  <div id="page-content-wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-12">
                    <div class="row extranet-outlet-wrapper">
                        <router-outlet></router-outlet>
                    </div>
                </div>
            </div>
        </div>
    </div>
</ng-template>

<ng-template #authenticate>
  <router-outlet></router-outlet>
</ng-template>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular - 周围的条件 div 容器 的相关文章

随机推荐