gpt4 book ai didi

Angular 4 在 ngIf 中使用公共(public)静态只读字符串

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

我有一个名为 UserRoles 的类,定义如下:

export class UserRoles
{
public static readonly MODERATOR = "moderator";
public static readonly SPONSOR = "sponsor";
public static readonly BASE_USER = "user";
}

我有一个名为 MenuComponent 的组件,因此在 .ts 文件中,我 import { UserRoles } from "../../../models/userRoles"; ,在该组件的 .html 文件中,我有:

<nav class="navbar navbar-inverse">
<div class="container-fluid">
<ul *ngIf="currentUser.role == UserRoles.BASE_USER" class="nav navbar-nav">
<!-- NOTE: whatever routerLinks you create here need to be added and handled properly in routing.ts -->
<li><button class="menuButton" routerLink="leaderboard">Leaderboard</button></li>
<li><button class="menuButton" routerLink="feed">Feed</button></li>
<li><button class="menuButton" routerLink="score">Score</button></li>
<li><button class="menuButton" routerLink="my-posts">My Posts</button></li>
<li><button class="menuButton" routerLink="create-posts">Create Posts</button></li>
<li><button class="menuButton" routerLink="edit-posts">Edit Posts</button></li>
</ul>
<ul *ngIf="currentUser.role == UserRoles.MODERATOR" class="nav navbar-nav">
<!-- NOTE: whatever routerLinks you create here need to be added and handled properly in routing.ts -->
<li><button class="menuButton" routerLink="flagged-users">Flagged Users</button></li>
<li><button class="menuButton" routerLink="flagged-posts">Flagged Posts</button></li>
<li><button class="menuButton" routerLink="flagged-comments">Flagged Comments</button></li>
<li><button class="menuButton" routerLink="my-messages">My Messages</button></li>
<li><button class="menuButton" routerLink="create-messages">Create Messages</button></li>
</ul>
<ul *ngIf="currentUser.role == UserRoles.SPONSOR" class="nav navbar-nav">
<!-- NOTE: whatever routerLinks you create here need to be added and handled properly in routing.ts -->
<li><button class="menuButton" routerLink="balance">Balance</button></li>
<li><button class="menuButton" routerLink="popular-users">Popular Users</button></li>
<li><button class="menuButton" routerLink="my-advertisements">My Advertisements</button></li>
<li><button class="menuButton" routerLink="create-advertisements">Create Advertisements</button></li>
</ul>
</div>
</nav>

当我启动 ng serve 并对其进行测试时,以上都没有呈现,当我检查 Developer Console 时,我收到的消息基本上告诉我 UserRoles 未定义。

对于 UserRoles 类,我应该在此处或 app.module.ts 中做什么?

最佳答案

模板中的表达式期望使用组件类的变量和方法。为了访问模板中 UserRoles 的值,您可以将该类分配给组件类的成员变量:

import { Component } from '@angular/core';
import { UserRoles } from "./user-roles.model";

@Component(
...
)
export class UserComponent {
public UserRolesClass = UserRoles;
....
}

并在模板中使用该变量:

<ul *ngIf="currentUser.role == UserRolesClass.BASE_USER" class="nav navbar-nav">

该技术显示在 this stackblitz 中.

关于Angular 4 在 ngIf 中使用公共(public)静态只读字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48039523/

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