我对 Angular 2 Final Release 路由有一些问题。当我在子组件中添加 [routerLink] 时,会发生此错误:
Can't bind to 'routerLink' since it isn't a known property of 'button'
这是我的树:
app
|
|___ app.routes.ts
|___ app.module.ts
|
|___ user
|_______ user.routes.ts
|_______ user.module.ts
|_______ user.component.ts
|
|_______ login
|____________ login.module.ts
|____________ login.component.html
|____________ login.component.ts
现在文件内容:
应用程序.routes.ts
import { Routes } from '@angular/router';
import { UserRoutes } from './user/user.component';
export const routes: Routes = [
...UserRoutes
];
app.module.ts
import { APP_BASE_HREF } from '@angular/common';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { routes } from './app.routes';
// Modules
import { UserModule } from './user/user.module';
@NgModule({
imports: [BrowserModule, HttpModule, RouterModule.forRoot(routes), UserModule],
declarations: [AppComponent],
providers: [
{
provide: APP_BASE_HREF,
useValue: '<%= APP_BASE %>'
},
],
bootstrap: [AppComponent]
})
export class AppModule { }
用户.routes.ts
import { Route } from '@angular/router';
import { UserComponent } from './user.component';
import { LoginComponent } from './login/login.component';
export const UserRoutes: Route[] = [
{
path: '',
component: UserComponent,
children: [
{
path: 'login',
component: LoginComponent
}
]
}
];
用户模块.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { UserComponent } from './user.component';
import { LoginModule } from './login/login.module';
@NgModule({
imports: [CommonModule, RouterModule, LoginModule],
declarations: [UserComponent],
exports: [UserComponent]
})
export class UserModule { }
用户.component.ts
import { Component } from '@angular/core';
/**
* This class represents the lazy loaded UserComponent.
*/
@Component({
selector: 'user',
template: '<router-outlet></router-outlet>',
})
export class UserComponent { }
登录模块.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule } from '@angular/forms';
import { LoginComponent } from './login.component';
@NgModule({
imports: [CommonModule, ReactiveFormsModule],
declarations: [LoginComponent],
exports: [LoginComponent]
})
export class LoginModule { }
login.component.html
<button [routerLink]="['/']">Back home</button>
在 login.component.html 上,如果我删除按钮,一切正常。此外,如果我在user.component.ts中添加按钮,它可以工作:
import { Component } from '@angular/core';
/**
* This class represents the lazy loaded UserComponent.
*/
@Component({
selector: 'user',
template: '<button [routerLink]="[\'/\']">Back home</button>',
})
export class UserComponent { }