我正在使用AngularJS 2創建SPA應用程序。我正在嘗試向活動路徑的按鈕添加一個類。根據Angular 2's router documentation我應該使用RouterLinkActive
指令來添加一個類。但是,當我這樣做時,我得到Cannot read property 'root' of undefined
錯誤。如果我從模板中刪除routerLinkActive="active"
,該應用完美地工作。在Angular 2中使用routerLinkeActive時出現未定義的根錯誤
路由器代碼:
export const routes: RouterConfig = [
{path : '', redirectTo: 'home', pathMatch: 'full'},
{path : 'home', component : HomeComponent}
{path : 'item', component : ItemListComponent}
{path : 'item/:id', component : ItemComponent},
];
export const APP_ROUTER_PROVIDERS = [
provideRouter(routes)
];
HTML模板代碼:
<button [routerLink]="['home']" class="btn btn-block" routerLinkActive="active">Home</button>
<button [routerLink]="['item']" class="btn btn-block">Items</button>
組件代碼:
@Component({
selector: 'home-page',
directives: [ROUTER_DIRECTIVES],
precompile: [HomeComponent, ItemListComponent],
templateUrl: `app/templates/main-page.template.html`
})
export class HomePageComponent { }
錯誤:
EXCEPTION: Error in app/templates/main-page.template.html:15:8
ORIGINAL EXCEPTION: TypeError: Cannot read property 'root' of undefined
什麼是根源,我應該在哪裏定義它?
@不知道這個!完成我的回答! – Fals