模板解析错误:无法绑定到'routerLink',因为它不是'a'的已知属性

时间:2016-12-21 17:22:36

标签: angular angular2-routing angular2-template angular2-directives

我正在创建一个组件然后它被设置为root组件然后我收到错误Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'.我已经使用RouterLink作为组件anotation中的指令但它无法工作。

app.routes.ts

import { ModuleWithProviders }  from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { dogRoutes }    from './Dogs/dog.routes';
import { catRoutes }    from './Cats/cat.routes';
import {userRoutes} from "./Users/user.routes";
// Route Configuration
export const routes: Routes = [
  {
    path: '',
    redirectTo: '/dogs',
    pathMatch: 'full'
  },
  ...catRoutes,
  ...dogRoutes,
  ...userRoutes
];

export const routing: ModuleWithProviders = RouterModule.forRoot(routes);

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
  <div class="demo-layout-transparent mdl-layout mdl-js-layout">
  <header class="mdl-layout__header mdl-layout__header--transparent">
    <div class="mdl-layout__header-row">
      <!-- Title -->
      <span class="mdl-layout-title">Scotch Pets</span>
      <!-- Add spacer, to align navigation to the right -->
      <div class="mdl-layout-spacer"></div>
      <!-- Navigation with router directives-->
      <nav class="mdl-navigation">
        <a class="mdl-navigation__link" [routerLink]="['/']">Home</a>
        <a class="mdl-navigation__link" [routerLink]="['/cats']">Cats</a>
        <a class="mdl-navigation__link" [routerLink]="['/dogs']">Dogs</a>
        <a class="mdl-navigation__link" [routerLink]="['/login']">Login</a>
      </nav>
    </div>
  </header>
  <main class="mdl-layout__content">
    <h1 class="header-text">We care about pets...</h1>
  </main>
</div>
<!-- Router Outlet -->
<div class="container">
  <router-outlet></router-outlet>
    </div>
  `,

})

export class AppComponent{}

app.module.ts

import { NgModule }       from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { FormsModule }    from '@angular/forms';
import { HttpModule, JsonpModule } from '@angular/http';

import { AppComponent }         from './app.component';
import { DogsModule } from './Dogs/dogs.module';
import { CatsModule } from './Cats/cats.module';
import { UserModule }          from './Users/users.module';
import {routing} from './app.routes';
import {RouterModule} from "@angular/router";


@NgModule({
  imports: [
    BrowserModule,
FormsModule,
 HttpModule,
RouterModule,
JsonpModule,
UserModule,
DogsModule,
CatsModule,
routing
 ],
 declarations: [
  AppComponent,
 ],
 providers: [   ],
  bootstrap: [ AppComponent ]
})

export class AppModule {}

狗list.component.ts

 import {Component, OnInit} from '@angular/core';
import {PetService} from '../services/pet.service'
import {Observable} from 'rxjs/Observable';
import {Pet} from '../model/pet';
import {AuthenticationService} from "../../Users/services/authentication.service";


@Component({

  template: `
    <h2>Dogs</h2>
    <p>List of dogs</p>
    <ul class="demo-list-icon mdl-list">
      <li class="mdl-list__item" *ngFor="let dog of dogs | async">
        <span class="mdl-list__item-primary-content">
            <i class="material-icons mdl-list__item-icon">pets</i>
            <a [routerLink]="['/dogs', dog.id.$t]">{{dog.name.$t}}</a>
        </span>
      </li>
    </ul>
    `
})
// Component class implementing OnInit
export class DogListComponent implements OnInit {
  // Private property for binding
  dogs: Observable<Pet[]>;

  constructor(private petService: PetService, private _service: AuthenticationService) {

  }

  // Load data ones componet is ready
  ngOnInit() {
    this._service.checkCredentials();
    // Pass retreived pets to the property
    this.dogs = this.petService.findPets('dog');
    alert(JSON.stringify(this.dogs))
  }
}

有没有更好的方法来添加修复此错误?

3 个答案:

答案 0 :(得分:12)

您需要将RouterModule添加到每个模块的imports: [] ,其中您使用路由器指令,例如RouterOutletrouterLink -

答案 1 :(得分:5)

您必须在组件中导入路由器才能使用routerLink。

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

您需要将RouterModule添加到导入:[],使用路由器指令(如RouterOutlet或routerLink)的每个模块。

答案 2 :(得分:2)

我使用Angular Material2(&#34; 2.0.0-alpha.9-3&#34;,)md-nav-list遇到了这个问题:

<md-nav-list>
  <md-list-item>
    <a [routerlink]="['']">
      <md-icon md-list-avatar>home</md-icon>
      <span>Home</span>
    </a>
  </md-list-item>
</md-nav-list>

引发Can't bind to 'routerlink' since it isn't a known property of 'a'.

<md-nav-list>
  <md-list-item>
    <a [routerLink]="['']">
      <md-icon md-list-avatar>home</md-icon>
      <span>Home</span>
    </a>
  </md-list-item>
</md-nav-list>

工作正常。请注意,只有routerlinkrouterLink的情况不同。就我而言,IntelliJ会自动自动转换为全小写版本......呃!