Angular2获取当前路线的别名 [英] Angular2 get current route's alias

查看:79
本文介绍了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.

根据您的活动路线,以下内容将打印FooBar.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆