简体   繁体   English

带有按钮和 *ngif 的 RouterLink

[英]RouterLink with button and *ngif

I am using *ngIf to route to a different page based on a variable.我正在使用 *ngIf 基于变量路由到不同的页面。 When I click on the button nothing happens.当我单击按钮时,没有任何反应。 The links does exists.链接确实存在。 This is my code:这是我的代码:

        <button mat-button   >
          <span class="label" >Micro Memoirs</span>
          <div *ngIf="signedIn else elseBlock">
            <a [routerLink]="['/story']"></a>
          </div>
          <ng-template #elseBlock>
          <a [routerLink]="['/auth']">  </a>
          </ng-template>
       </button>

You don't necessarily need the *ngIf directive.您不一定需要*ngIf指令。 You could include the condition directly in the [routerLink] binding.您可以将条件直接包含在[routerLink]绑定中。 Try the following尝试以下

<button mat-button [routerLink]="signedIn ? ['/story'] : ['/auth']">
  <span class="label" >Micro Memoirs</span>
</button>

One quick way to remember this is that whenever property binding [] or interpolation {{ }} is involved, they are interpreted as expressions.记住这一点的一种快速方法是,无论何时涉及属性绑定[]或插值{{ }} ,它们都被解释为表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM