我能够获得当前路径的路径,例如:/ about,/,/ news with location.path() . 但是我怎样才能得到它的别名(路由定义中的'as'部分)?
{ path: '/about', as: 'About', component: About }
可能吗?
5 回答
1
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.
5 回答
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
.无法看到获取它的方法,但另一种方法是使用RouteData传递路由的别名
https://angular.io/docs/ts/latest/api/router/RouteData-class.html
您不应该使用位置,而应使用路由器实例 .
在组件中,将其注入构造函数中:
比,你可以获得组件的名称:
我不确定你是如何从路由器配置获得 as 的 . 但是,RouterLink指令应该是正确的开始:https://angular.io/docs/ts/latest/api/router/RouterLink-directive.html
For >= Angular2 RC.x (new router)
新路由器中不再有别名 .
您可以获取本地组件的相对路径
或使用的完整路径
要么
如果您知道可能的别名列表,那么可以使用
router.generate
和route.isActiveRoute
的组合找出当前别名的名称:例如,我有一个包含三个步骤的向导 . 我有一个数组中的步骤别名列表:
然后,我使用以下代码来确定当前别名:
免责声明:我没有试过看看它是否适用于路由参数 .