【问题标题】:Show html depending on the current route params根据当前路由参数显示 html
【发布时间】:2019-06-12 21:00:57
【问题描述】:

在我的组件中,我想根据当前的路由参数显示一个文本,在模板中我做了如下:

<h1 *ngIf="mode === 'edit'">Modifier</h1>
<h1 *ngIf="mode === 'add'">Ajouter</h1>

在 ngOnInit 函数中我有这个:

this.route.params.subscribe(params => {
    this.id = +params['id'];
    this.currentMode = params['mode'];
})

但是当我访问我的组件时,尽管设置了 currentMode,但该元素始终是隐藏的。

我该如何解决这个问题?

【问题讨论】:

  • 那么你也应该在你的模板中检查currentMode。目前您正在检查mode
  • 您不应该使用 currentMode 而不是仅使用 mode 吗? *ngIf="currentMode === 'edit'"

标签: angular


【解决方案1】:

你不能在模板中使用param名称,使用变量名称,即this.currentMode

<h1 *ngIf="currentMode === 'edit'">Modifier</h1>
<h1 *ngIf="currentMode === 'add'">Ajouter</h1>

这里的假设是您从变量this.currentMode 中的参数中获取值,我将通过在标签旁边打印它来测试它:

{{currentMode}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2013-06-20
    • 2018-06-08
    • 2018-10-12
    • 1970-01-01
    • 2021-06-30
    • 2021-12-17
    相关资源
    最近更新 更多