angular 2 rc5 模板解析错误 [英] angular 2 rc5 template parse error
问题描述
我在尝试创建角度路由链接时不断收到此错误:
I keep getting this error when I'm trying to create an angular routing link:
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>
"): AppComponent@4: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>
在我的组件中,我有这个:
and in my component I have this:
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),这也不起作用.
following this tutorial (https://angular.io/docs/ts/latest/guide/router.html#!#router-link) and that doesn't work either.
这就是我引导我的应用程序的方式:
This is how I am bootstrapping my app:
@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
})]);
推荐答案
你不需要包含指令:[RouterLink]
,使用[routerLink]
.您可以通过 RouterModule.forRoot
进行设置.
You don't need to include directives: [RouterLink]
, To use [routerLink]
. It will be available via the setup you do via RouterModule.forRoot
.
@NgModule({
...
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(<route paths and configurations>)
],
....
})
在任何功能模块中,您必须通过添加RouterModule
来显式导入它.
In any Feature Module, you have to explicitly import it by adding RouterModule
.
希望这有帮助!!
这篇关于angular 2 rc5 模板解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!