0

我正在尝试动态创建带有 url 列表的链接:

<span *ngIf="item.type=='link'">
          <a  class="nav-link" [routerLink]="item.routerLink" skipLocationChange>{{item.label}}</a>
        </span>

当我在本地主机中运行 dev 时,此解决方案运行良好,但在尝试构建库时出现以下错误:

RROR: Can't bind to 'routerLink' since it isn't a known property of 'a'. ("<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
  <a class="navbar-brand" [ERROR ->][routerLink]="navBar.routerLink" skipLocationChange><img src="{{navBar.imagePath}}" height="40px" wid")
Can't bind to 'routerLink' since it isn't a known property of 'a'. ("item':'nav-item dropdown'">
        <span *ngIf="item.type=='link'">
          <a  class="nav-link" [ERROR ->][routerLink]="item.routerLink" skipLocationChange>{{item.label}}</a>
        </span>

这是我的依赖项列表:

"@angular/animations": "~8.2.9",
    "@angular/common": "~8.2.9",
    "@angular/compiler": "~8.2.9",
    "@angular/core": "~8.2.9",
    "@angular/forms": "~8.2.9",
    "@angular/platform-browser": "~8.2.9",
    "@angular/platform-browser-dynamic": "~8.2.9",
    "@angular/router": "~8.2.9",
4

1 回答 1

0

你进口了import {RouterModule} from '@angular/router';吗? RouterModule这应该可以工作。或者另一种制作动态路由的方法是你可以调用函数并在你的component.ts文件中管理它。 例子

import { Router } from '@angular/router';

export class ExampleComponent {

    constructor(private router: Router,) {}

    routerLinkTo() {
        this.router.navigate(["/foo"]);
    }
}

请在此处查看此示例

有关更多路由器功能,请在此处查看 Angular 文档

于 2021-02-24T08:00:12.183 回答