gpt4 book ai didi

Angular2 获取当前路径的别名

转载 作者:太空狗 更新时间:2023-10-29 17:01:26 25 4
gpt4 key购买 nike

我能够使用 location.path() 获取当前路线的路径,例如:/about、/、/news。但是我怎样才能得到它的别名(路由定义中的“as”部分)?

{ path: '/about', as: 'About', component: About }

这可能吗?

最佳答案

注意:以下内容已经过 2.0 beta 系列测试。 RC 版本具有更新的路由器组件,其中包含重大更改。旧的已重命名为 router-deprecated。这尚未针对新路由器进行测试。

以下将根据您的事件路线打印 FooBar

@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 获取当前路径的别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34479405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com