如何解决错误 NG8002:无法绑定到“routerLink”,因为它不是“a”的已知属性。?
Posted
技术标签:
【中文标题】如何解决错误 NG8002:无法绑定到“routerLink”,因为它不是“a”的已知属性。?【英文标题】:How to solve error NG8002: Can't bind to 'routerLink' since it isn't a known property of 'a'.? 【发布时间】:2021-10-06 00:42:10 【问题描述】:我在 Angular 11 上工作,我在构建 Angular 应用程序时遇到错误
ERROR in src/app/Employee-list/employee-list.component.html:22:45 - error NG8002: Can't bind to
'routerLink' since it isn't a known property of 'a'.
<a class="nav-link text-dark" [routerLink]="['/employee-list']">Staff List</a>
下面是包json文件
"@angular/animations": "^11.2.14",
"@angular/cdk": "^12.1.3",
"@angular/common": "^11.0.0",
"@angular/compiler": "^11.0.0",
"@angular/core": "^11.0.0",
"@angular/forms": "^11.0.0",
"@angular/material": "^11.2.13",
"@angular/router": "^11.2.14",
"@angular/cli": "^11.0.0",
"@angular/compiler-cli": "^11.0.0"
在应用模块上我做如下:
import RouterModule from '@angular/router';
@NgModule(
imports: [
RouterModule
那么如何解决问题?
应用程序模块示例和组件员工列表作为打击存在
https://stackblitz.com/edit/angular-uffug5?file=src%2Fapp%2FEmployee-list%2Femployee-list.component.ts
问题图片
【问题讨论】:
这是一个很常见的问题,以下主题中的任何答案对您有帮助吗? Can't bind to 'routerLink' since it isn't a known property, Can't bind to 'routerLink' since it isn't a known property of 'a', Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a' 感谢您的回复,我已经在应用程序模块的导入部分添加了 RouterModule,所以请您帮我解决这个问题 能否请您发布您的整个 app.module 以及您的employeeList HTML 文件和employeeList 组件文件? 这完全是 app.module 和 employeecomponent stackblitz.com/edit/… 我把它贴在 app.module 和组件的employeelist 你得到了吗 【参考方案1】:您要么必须将[routerLink]="['/employee-list']"
更改为routerLink="/employee-list"
或者你必须像这样在你的组件中设置一个属性并更改路由器链接:
export class yourComponent
public yourRouterLink= "/employee-list"; // Here is your employee-list
<a class="nav-link text-dark" [routerLink]="yourRouterLink">Staff List</a>
【讨论】:
以上是关于如何解决错误 NG8002:无法绑定到“routerLink”,因为它不是“a”的已知属性。?的主要内容,如果未能解决你的问题,请参考以下文章
ng-Route 没有加载视图,显示一个空白屏幕,没有任何错误
NG0303:无法绑定到“ngModel”,因为它不是“离子范围”的已知属性
Angular TestBed:无法绑定到“ngModel”,因为它不是“ng-select”的已知属性
无法将web api服务数据绑定到角度为2的ng2-smart-table