Angular 7 无法绑定到“routerlink”,因为它不是“a”的已知属性

Posted

技术标签:

【中文标题】Angular 7 无法绑定到“routerlink”,因为它不是“a”的已知属性【英文标题】:Angular 7 and can't bind to 'routerlink' since it isn't a known property of 'a' 【发布时间】:2019-04-08 04:45:21 【问题描述】:

我正在创建一个新的 Angular 7 应用,过去几天我一直在研究这个问题,但我无法找到解决这个错误的方法:

无法绑定到“routerlink”,因为它不是“a”的已知属性

这是我看过的一些文章 Angular2 Exception: Can't bind to 'routerLink' since it isn't a known native property 和 Getting Can't bind to 'routerLink' since it isn't a known property of 'a'. error in spite of referencing router moudule 但“不行”。

这是布局:

应用程序 -> 导航 代码如下:

app.module.ts

import  BrowserModule  from '@angular/platform-browser';
import  NgModule  from '@angular/core';
import  UltiMaterialModule  from './ulti-material/ulti-material.module';
import  BrowserAnimationsModule  from '@angular/platform-browser/animations';
import  HttpClientModule  from '@angular/common/http';
import  LayoutModule  from '@angular/cdk/layout';
import  NavigationModule  from './navigation/navigation.module';
import  AppRoutingModule  from './app-routing.module';
import  RouterModule  from '@angular/router';

import  AppComponent  from './app.component';

@NgModule(
  declarations: [
    AppComponent
  ],
  imports: [
    RouterModule,
    AppRoutingModule,
    BrowserModule,
    BrowserAnimationsModule,
    LayoutModule,
    HttpClientModule,
    UltiMaterialModule, // ulti-nav/ulti-nav.component.ts
    NavigationModule
  ],
  providers: [],
  bootstrap: [AppComponent]
)
export class AppModule  

app-routing.module.ts

import  NgModule  from '@angular/core';
import  RouterModule, Routes  from '@angular/router';

export const AppRoutes: Routes = [
   path: '**',  loadChildren: './navigation/navigation.module#NavigationModule', pathMatch: 'full' 
];

@NgModule(
  imports: [ RouterModule.forRoot(AppRoutes) ],
  exports: [ RouterModule ]
)
export class AppRoutingModule 

app.component.ts

import  Component  from '@angular/core';

@Component(
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
)
export class AppComponent 

app.component.html

<!--The content below is only a placeholder and can be replaced.-->
<app-navigation></app-navigation>

导航/navigation.module.ts

import  NgModule  from '@angular/core';
import  CommonModule  from '@angular/common';

import  RouterModule, RouterLink  from '@angular/router';

import  BrowserModule  from '@angular/platform-browser';

import  UltiMaterialModule  from '../ulti-material/ulti-material.module';

import  VendorsComponent  from '../vendors/vendors.component';
import  DashboardComponent  from '../dashboard/dashboard.component';
import  UserPanelComponent  from '../user-panel/user-panel.component';
import  QuestionnaireComponent  from '../questionnaire/questionnaire.component';
import  NavigationComponent  from './navigation.component';

import  NavigationRoutingModule  from './navigation-routing.module';


@NgModule(
  declarations: [
    VendorsComponent,
    DashboardComponent,
    UserPanelComponent,
    QuestionnaireComponent,
    NavigationComponent
  ],
  imports: [
    RouterModule,
    BrowserModule,
    CommonModule,
    UltiMaterialModule,
    NavigationRoutingModule
  ],
  exports: [
    NavigationComponent,
    RouterModule
  ]
)
export class NavigationModule 

导航/导航路由器.module.ts

import  NgModule  from '@angular/core';
import  RouterModule, Routes  from '@angular/router';
import  VendorsComponent  from '../vendors/vendors.component';
import  DashboardComponent  from '../dashboard/dashboard.component';

export const NavigationRoutes: Routes = [
   path: 'home', redirectTo: 'dashboard', pathMatch: 'full' ,
   path: 'vendors', component: VendorsComponent ,
   path: 'dashboard', component: DashboardComponent ,
   path: '', redirectTo: 'dashboard', pathMatch: 'full' 
];

@NgModule(
  imports: [ RouterModule.forChild(NavigationRoutes) ],
  exports: [ RouterModule ]
)
export class NavigationRoutingModule 

导航/navigation.component.ts

import  Component  from '@angular/core';
import  BreakpointObserver, Breakpoints, BreakpointState  from '@angular/cdk/layout';
import 
  Router,
  Event as RouterEvent,
  NavigationStart,
  NavigationEnd,
  NavigationCancel,
  NavigationError
 from '@angular/router';
import  ActivatedRoute  from '@angular/router';
import  Observable  from 'rxjs';
import  map  from 'rxjs/operators';

@Component(
  selector: 'app-navigation',
  templateUrl: './navigation.component.html',
  styleUrls: ['./navigation.component.scss']
)
export class NavigationComponent 

  // Native Template fails on Button mat-icon render with always false. Had to reconstruct based on:
  // https://***.com/questions/50525676/angular-6-material-nav-component-template-parse-errors-unexpected-token?rq=1
  /*
  isHandset$: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.Handset)
    .pipe(
      map(result => result.matches)
    );
  */

  isHandset$: Observable<BreakpointState> = this.breakpointObserver.observe(Breakpoints.Handset);

  _loading = true;
  public get showLoadingIndicator() 
    return this._loading;
  
  public set showLoadingIndicator(value) 
    this._loading = value;
  

  constructor(private breakpointObserver: BreakpointObserver,
    private router: Router,
    private route: ActivatedRoute
    ) 

    router.events.subscribe((event: RouterEvent) => 
      this.navigationInterceptor(event);
    );
  

  // Shows and hides the loading spinner during RouterEvent changes
  navigationInterceptor(event: RouterEvent): void 
    if (event instanceof NavigationStart) 
      this._loading = true;
    
    if (event instanceof NavigationEnd) 
      this._loading = false;
    

    // Set loading state to false in both of the below events to hide the spinner in case a request fails
    if (event instanceof NavigationCancel) 
      this._loading = false;
    
    if (event instanceof NavigationError) 
      this._loading = false;
    
  

navigation.component.html

<div *ngIf="showLoadingIndicator" class="loading-indicator"><mat-spinner></mat-spinner></div>
<mat-toolbar color="primary">
    <button 
      type="button"
      aria-label="Toggle sidenav"
      mat-icon-button
      (click)="drawer.toggle()"
      *ngIf="isHandset$ | async">
      <mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
    </button>
    <span><mat-icon class="logo" svgIcon="logo2"></mat-icon></span>
    <span class="span-fill-remaining-space"></span>
    <span>Global Security Research Project System</span>
    <button 
      type="button"
      aria-label="Toggle sidenav"
      mat-icon-button
      (click)="user.toggle()"
      *ngIf="isHandset$ | async">
      <mat-icon aria-label="Side nav toggle icon">account_circle</mat-icon>
    </button>
</mat-toolbar>
<mat-sidenav-container class="sidenav-container">
  <mat-sidenav
    #drawer
    class="sidenav"
    fixedInViewport="false"
    fixedTopGap="64"
    [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
    [mode]="(isHandset$ | async) ? 'over' : 'side'"
    [opened]="!(isHandset$ | async)">
    <mat-nav-list>
      <a mat-list-item >
        <mat-icon class="icon">home</mat-icon>
        <span class="label">Home</span>
      </a>
      <a mat-list-item >
        <mat-icon class="developer_board">developer_board</mat-icon>
        <span class="label">Project</span>
      </a>
      <a mat-list-item >
        <mat-icon class="how_to_reg">how_to_reg</mat-icon>
        <span class="label">Products</span>
      </a>
      <a mat-list-item>
        <mat-icon class="people">people</mat-icon>
        <span class="label">Teams</span>
      </a>
      <a mat-list-item [routerlink]="['/vendors']" routerLinkActive>
        <mat-icon class="store">store</mat-icon>
        <span class="label">Vendors</span>
      </a>
      <a mat-list-item>
        <mat-icon class="dashboard">dashboard</mat-icon>
        <span class="label">Dashboard</span>
      </a>
      <a mat-list-item>
        <mat-icon class="question_answer">question_answer</mat-icon>
        <span class="label">Questionnaire</span>
      </a>
    </mat-nav-list>
  </mat-sidenav>
  <mat-sidenav 
    #user
    position="end">
    <app-user-panel></app-user-panel>
  </mat-sidenav>
  <mat-sidenav-content>
    <div class="material-page-spacing">
      <router-outlet></router-outlet>
    </div>
  </mat-sidenav-content>
</mat-sidenav-container>

package.json - 依赖项

"@angular/animations": "^7.0.2",
"@angular/cdk": "^7.0.2",
"@angular/common": "^7.0.2",
"@angular/compiler": "^7.0.2",
"@angular/core": "^7.0.2",
"@angular/forms": "^7.0.2",
"@angular/http": "^7.0.2",
"@angular/material": "^7.0.2",
"@angular/platform-browser": "^7.0.2",
"@angular/platform-browser-dynamic": "^7.0.2",
"@angular/router": "^7.0.2",
"angular-oauth2-oidc": "^5.0.2",
"core-js": "^2.5.4",
"material-design-icons": "^3.0.1",
"rxjs": "~6.3.3",
"speed-measure-webpack-plugin": "^1.2.3",
"zone.js": "~0.8.26"

我不知道问题出在哪里。据我所知,我有适当的进出口。如果您看到我看不到的内容,请随时发表评论。

如果您需要更多信息,例如更多代码,也请告诉我。

【问题讨论】:

【参考方案1】:

属性routerLink 区分大小写。

[routerlink] 更改为 [routerLink]

[routerLink]="['/vendors']"

【讨论】:

哇,不敢相信我没有看到。这解决了问题。我的错误 我没有对你投反对票。我试图投票给你,但我的名声不允许我这样做 另外,如果你传递一个常量字符串,你不需要括号:routerLink="/vendors"【参考方案2】:

对于那些将routerLinkcamel 大小写正确但仍然出现相同错误的人,您可能没有在定义组件的模块中导入RouterModule

import RouterModule from '@angular/router';
@NgModule(
   declarations:[YourComponents],
   imports:[RouterModule]
...

【讨论】:

这对我的案子有很大帮助。好点。【参考方案3】:

我遇到了类似的错误“无法绑定到 'routerlink',因为它不是 'button' 的已知属性”。

将模块 RouterTestingModule.withRoutes([]) 添加到规范类解决了我的问题。

【讨论】:

以上是关于Angular 7 无法绑定到“routerlink”,因为它不是“a”的已知属性的主要内容,如果未能解决你的问题,请参考以下文章

Angular 7 - ControlValueAccessor - 修剪绑定到表单的输入值

无法将 [(ngModel)] 绑定到 Angular html

无法绑定到“gridOptions”,因为它不是“ag-grid-angular”的已知属性

Angular 2 错误:无法绑定到“innerhtml”,因为它不是已知的本机属性

Angular 2 - 无法绑定到“ngModel”,因为它不是“输入”的已知属性

Angular 2 RC 5 - 无法绑定到“...”,因为它不是“...”的已知属性