简体   繁体   English

在儿童路线Angular 2之间导航

[英]Navigating between child routes Angular 2

I can't seem to be able to navigate between child routes. 我似乎无法在子路线之间导航。

Router overview: 路由器概述:

path: 'parent', component: parentComponent,
        children: [
            { path: '', redirectTo: 'child1', pathMatch: 'full' },
            { path: 'child1', component: child1Component },
            { path: 'child2', component: child2Component },
            { path: 'new/:type', component: child3Component },

            { path: '**', component: PageNotFoundComponent }
        ]

i'm in the child1 component trying to navigate to the new/type route like so: 我在child1组件中尝试导航到新的/类型路由,如下所示:

this._router.navigate(['new', objType], { relativeTo: this._route });

But i keep getting Error: Cannot match any routes. 但我一直得到错误:无法匹配任何路线。 URL Segment: 'new/asset'. 网址细分:'新/资产'。

If i manually insert the url it works fine. 如果我手动插入网址,它工作正常。

Help would be greatly appreciated, thanks! 非常感谢帮助,谢谢!

你可以用../

this._router.navigate(['../new', objType], { relativeTo: this._route });
this.router.navigate(['../../new'], { relativeTo: this._route })

Or 要么

this.router.navigate(['/parent/new'])

Or with anchor tag : 或者使用锚标记:

 <a [routerLink]=" ['../../new'] ">
      Go to new
 </a>

Or : 要么 :

 <a [routerLink]=" ['/parent/new'] ">
      Go to new
 </a>

When you use relativeTo and then try to navigate you should put in the relative path, not just the whole one from the parents viewpoint. 当您使用relativeTo然后尝试导航时,您应该放入相对路径,而不仅仅是父项视点中的整个路径。 In your example it should be more like: 在你的例子中,它应该更像是:

this._router.navigate([ '../', objType ], { relativeTo: this._route });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM