Angular 2新(RC1)路由器.嵌套路线

编程入门 行业动态 更新时间:2024-10-25 06:30:08
本文介绍了Angular 2新(RC1)路由器.嵌套路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在子组件的beta版本中(假设为'child1'),您可以告诉您的父级加载(导航到)其他子组件,而不是通过类似的方式加载当前子组件:

In beta version from the child component (let's say 'child1') you could tell your parent to load (navigate to) a different child component instead of current by something like that:

_router.parent.navigate(['child2']); ('_router' is injected in constructor)

在RC1中,不再有'parent'属性,因此您似乎必须使用_router.navigate()并从应用程序根目录开始提供完整的url.

In RC1 there's no 'parent' property anymore so it seems like the you have to use _router.navigate() and provide the full url starting from the app root. Something like

/root/grandparent/parent/child2

现在,问题是子组件是否仅了解父级的路由而不是祖父母级的路由-您如何做到这一点? 使用_router.navigate(['../child2'])会很好,但这给了我一个奇怪的错误,例如"invalid number of '../'.

Now, the question is if the child component is only aware about parent's routes but not about grandparent - how do you do this? Would be nice to use _router.navigate(['../child2']) but that gives me a weird errors like "invalid number of '../'.

到目前为止,我发现获得父母网址的唯一方法是:

The only way to get parent's url I found so far is that:

_router.routeTree._root.children[0].value.stringifiedUrlSegments

(您也可以通过OnActivate事件到达那里)

(you can also get there from OnActivate event)

但这对我来说似乎是错误的.可能还会有所不同,具体取决于父母/祖父母的数量-我还没有尝试过.

but this looks just wrong to me. It also might differ depending on the number of parents/grandparents - I haven't tried yet.

那么,有没有更好的方法可以做到这一点,或者相对路径应该起作用,而我做错了什么?

So, is there a better way to do that or relative path should work and I'm doing something wrong?

谢谢.

推荐答案

我遇到了同样的问题.就在今天,我找到了解决方案(它位于router.ts文件此处行:89)

I had the same problem. Just today I have found solution (it was in router.ts file here line:89)

/** * Navigate based on the provided array of commands and a starting point. * If no segment is provided, the navigation is absolute. * * ### Usage * * ``` * router.navigate(['team', 33, 'team', '11], segment); * ``` */ navigate(commands: any[], segment?: RouteSegment): Promise<void> { return this._navigate(this.createUrlTree(commands, segment)); }

您需要导入RouteSegment并将其添加到您的构造函数中

You need to import RouteSegment and add it you your constructor

import { Router, RouteSegment } from '@angular/router'; ... contructor(private $_router:Router, private $_segment:RouteSegment){} ... //If you are in for example child1 and child 1 is sibling of child2 this.$_router.navigate(['../child2'],this.$_segment);

更多推荐

Angular 2新(RC1)路由器.嵌套路线

本文发布于:2023-11-25 17:18:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   路由器   路线   Angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!