如何从ActivatedRoute获取有角度的5条航路段? [英] How to get angular 5 route segments from ActivatedRoute?

查看:56
本文介绍了如何从ActivatedRoute获取有角度的5条航路段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新的angular5版本.我有以下编码路线(请注意,水果和蔬菜不是参数:

I am using the latest angular5 version. I have the following coded routes (note that fruits and veggies are NOT parameters:

/fruits/item
/veggies/basket

请注意,水果是一条父路线,我在数组中有多个子路线(项目只是其中之一). 蔬菜"也是一条母路线. 篮子"是蔬菜路线下数组中的子路线"之一.

Note that fruits is a parent route, and I have multiple children routes in an array (item is just one of them). 'veggies' is also a parent route. 'basket' is one of the 'child routes' in an array under the veggies route.

我如何使用"ActivatedRoute",但如何访问路线的第一个水果"或蔬菜"部分?我想检查一下并注销到控制台.

How can I use "ActivatedRoute", though how do I get access to the first "fruits" or "veggies" segment of the route? I want to check it and log it out to the console.

假定已激活的路径已注入变量"route".

Assume Activated route is already injected into the variable "route".

正在寻找类似这样的伪代码(如果正确的话)

Looking for something like this pseudocode(if it is correct)

showRouteRoot() {
   console.log(this.route.getFirstSegmentNameInRoute()) 
// expected output is fruits OR veggies. 
// If this can't be done in one line of code, 
// multiple lines is an acceptable answer
}

我尝试过:

console.log(this.route.snapshot.url.split('/'));

这会产生问题.

console.log(this.route.url)

这向我展示了一些AnonymousSubject

This shows me some AnonymousSubject

console.log(this.route.parent.url)

这显示了一个BehaviorSubject

This shows me a BehaviorSubject

推荐答案

ActivatedRoute提供了成员pathFromRoot,该成员允许开发人员从根目录获取完整的activedroutes,请参考

ActivatedRoute provided a member pathFromRoot which let developers to get full activedroutes from root, refer DOCS.

根据您的情况,您可以使用下面的代码块来实现它:

For your situation, you can use below code block to achieve it:

this.activatedRoute.pathFromRoot[1].url.subscribe(val => console.log(val[0].path));

提到当前pathFromRoot的结果包含一个空路由(ActivatedRoute[]的第一个元素),这意味着路径''.

Mention that currently the result of pathFromRoot contains a empty route(first element of the ActivatedRoute[]) which means for the path ''.

这篇关于如何从ActivatedRoute获取有角度的5条航路段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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