angular - 在使用 Angular 6 时,我发现无法使用常量 typescript 文件作为路由路径

标签 angular

例如假设我有一个家庭组件

home.component.html, home.component.ts

在 home.component.html 文件中,我有要路由的 anchor 链接

<a id="routeDashboard" [routerLink]="['/dashboard']">Dashboard</a>

所以我想要的是 - 我们可以有一些恒定的路由路径文件,即从 html 文件引用,而不是直接在这里硬编码路由路径。

最佳答案

检查 Angular RouterLink ,您的代码应该是:

<a id="routeDashboard" [routerLink]="['/dashboard']">Dashboard</a>

<a id="routeDashboard" routerLink="/dashboard">Dashboard</a>

引用文档:

If the link is static, you can use the directive as follows: routerLink="/user/bob">link to user component

您可以将链接写入常量文件或 home.component.ts 文件 home.component.ts

links = [
    {id: 'routeDashborad', path: '/dashboard, name: 'Dashboard'},
    {id: 'routeProfile', path: '/profile', name: 'Profile'}
]

home.component.html

<div *ngFor="let link of links">
    <a [id]="link.id" [routerLink]="[link.path]">{{ link.name }}</a>
</div>

已添加:

如果我们将链接放入文件constants.ts

export class Constants {
    public static links = [{...}]
}

您可以将其导入到 home.component.ts 文件中:

import { Constants } from '/path/to/constants/file';

links = Constants.links;

关于angular - 在使用 Angular 6 时,我发现无法使用常量 typescript 文件作为路由路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56815039/

相关文章:

typescript - 导航栏中的路径名称与 Angular2 组件路由器中的子路由重复

javascript - 如何在 VS Code 的 Angular 工作流程中使用 Pusher-JS?

angular - 解密传入响应 Angular 9

angular - Lodash index.d.ts :244:12 Angular2 web pack build 中的重复标识符 '_'

Angular 4 在两个列表中搜索

Angular 5从web api ClosedXml下载excel文件

javascript - 如何在没有 window.location.reload 的情况下使用路由器刷新 Angular 页面?

javascript - 将类添加到 Angular2 指令中的下一个元素

angular - rxjs combineLatest 和 Anguar2 对 Observable 数组的变化检测

angular - 给出特定响应时取消订阅 Angular2 方法?