最近我读了this useful article about Angular 2 Router,看着the demo。一切似乎都很完美。但是,当我尝试根据router-link-active
类来确定活动路由时,我发现根路由始终处于活动状态。角2根的路线始终处于活动状态
这里是一块app.component.ts的代码,其中“主”路线被配置:
@Component({
selector: 'demo-app',
template: `
<a [routerLink]="['/']">Home</a>
<a [routerLink]="['/about']">About</a>
<div class="outer-outlet">
<router-outlet></router-outlet>
</div>
`,
// add our router directives we will be using
directives: [ROUTER_DIRECTIVES]
})
@Routes([
// these are our two routes
{ path: '/', name: 'Home', component: HomeComponent }, // , useAsDefault: true}, // coming soon
{ path: '/about', name: 'About', component: AboutComponent }
])
export class AppComponent { }
如果我改变到<a [routerLink]="['/home']">Home</a>
默认组件(从'/'
到'/home'
和<a [routerLink]="['/']">Home</a>
路径必须是HomeComponent)消失。 HomeComponent只有在链接被点击后才会被激活,并且每当我们改变到另一个路线时,router-link-active
将被正确添加和删除。
这是一个错误还是路由配置有问题?
此链接可能是你感兴趣的https://github.com/angular/angular/issues/8397 – yurzui
嗨@yurzui,感谢您的链接。这非常有帮助。原来,这就是Angular 2路线的作用方式。所以我必须做我自己的解决方法.. :( – asubanovsky
yurzui s链接似乎是关于新的路由器。对于测试版和RC.1'@ angular/router-deprecated' https://github.com/angular/angular/issues/5334似乎是相关的问题。 –