2016-08-24 96 views
1

我不斷收到當我試圖創建一個角路由鏈路此錯誤:角2 RC5模板語法錯誤

zone.js:461 Unhandled Promise rejection: Template parse errors: 
Can't bind to 'routerLink' since it isn't a known property of 'a'. (" 
</h1> 
<button (click)="doLogin()">Login</button> 
<a [ERROR ->][routerLink]="['/createservice']" href="#">Post a Service</a> 
<router-outlet></router-outlet> 
"): [email protected]:3 ; Zone: <root> ; Task: Promise.then ; Value: BaseExceptionconsoleError @ zone.js:461 
zone.js:463 Error: Uncaught (in promise): Template parse errors:(…) 

這是我的代碼:

<a [routerLink]="['/createservice']" href="#">Post a Service</a> 

,並在我的組件我有這樣的:

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

@Component({ 
    moduleId: module.id, 
    selector: 'app-root', 
    providers: [AngularFire], 
    templateUrl: 'app.component.html', 
    styleUrls: ['app.component.css'], 
    directives: [RouterLink] 
}) 

我也試過這樣:

<a routerLink="/createservice" routerLinkActive="active">Post a Service</a> 

按照本教程(https://angular.io/docs/ts/latest/guide/router.html#!#router-link),這也不起作用。

這是我如何自舉我的應用程序:

@NgModule({ 
    imports: [ 
    BrowserModule, 
    AngularFireModule.initializeApp(firebaseConfig), 
    RouterModule, 
    routing 
    ], 
    declarations: [ AppComponent, CreateServiceComponent ], 
    providers: [ appRoutingProviders ], 
    bootstrap: [ AppComponent,FIREBASE_PROVIDERS ] 
}) 
export class MyAppModule {} 

if (environment.production) { 
    enableProdMode(); 
} 

bootstrap(AppComponent, [ 
    FIREBASE_PROVIDERS, 
    defaultFirebase(firebaseConfig), 
    firebaseAuthConfig({ 
    provider: AuthProviders.Facebook, 
    method: AuthMethods.Popup 
})]); 
+0

您正在使用哪個angular2版本? –

+0

我正在使用rc5。 – Tyler

+0

你在使用@NgModule嗎?請發佈如何引導您的應用程序。 – j2L4e

回答

1

你並不需要包括directives: [RouterLink],要使用[routerLink]。它將通過您通過RouterModule.forRoot所做的設置進行使用。

@NgModule({ 
    ... 
    imports: [ 
    BrowserModule, 
    FormsModule, 
    RouterModule.forRoot(<route paths and configurations>) 
    ], 
    .... 
}) 

在任何功能模塊中,您必須通過添加RouterModule明確導入它。

希望這有助於!

+0

我已經有這個導出const路由= RouterModule.forRoot(appRoutes);在app.routes.ts中 – Tyler