Angular2 获取当前路由的别名 [英] Angular2 get current route's alias
本文介绍了Angular2 获取当前路由的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用 location.path() 获取当前路线的路径,例如:/about、/、/news.但是我怎样才能得到它的别名(路由定义中的as"部分)?
I'm able to get current route's path like: /about, /, /news with location.path(). But how can I get its alias instead (the 'as' part in the definition of a route)?
{ path: '/about', as: 'About', component: About }
有可能吗?
推荐答案
注意:以下内容已经过 2.0 beta 系列的测试.RC 版本有一个更新的路由器组件,其中包含重大更改.旧的已重命名为 router-deprecated
.这尚未针对新路由器进行测试.
NOTE: The following has been tested with the 2.0 beta series. The RC versions have an updated router component with breaking changes. The old one has been renamed to router-deprecated
. This has not been tested yet against the new router.
以下将根据您的活动路线打印 Foo
或 Bar
.
The following will print Foo
or Bar
depending on your active route.
@Component({
selector: 'app',
templateUrl: 'app/app.html',
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path:'/', name: 'Foo', component: FooComponent, useAsDefault: true},
{path:'/bar', name: 'Bar', component: BarComponent, useAsDefault: false},
])
export class AppComponent implements OnInit {
constructor(private _router: Router) {
}
ngOnInit() {
console.log('current route name',
this._router.currentInstruction.component.routeName);
}
}
这篇关于Angular2 获取当前路由的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文