[英]how to input two route parameters one of which is in between the URL for navigation while using children routes in Angular CLI 8+ using Angular Router
I am trying to navigate to a page providing two route parameters as follows:我正在尝试导航到提供两个路由参数的页面,如下所示:
author/journals/:journal_Id/draftArticles/:articleId
I know that two parameters can be given when route parameter is at the end of the route like author/journals/draftArticles/:journal_Id/:articleId, journal_Id, articleId
and this will probably solve my issue.我知道当路由参数位于路由的末尾时可以给出两个参数,如author/journals/draftArticles/:journal_Id/:articleId, journal_Id, articleId
这可能会解决我的问题。 But I wanted to maintain the structure I am trying.但我想保持我正在尝试的结构。
path: 'author',
canActivate: [AuthGuardService],
canActivateChild: [AuthorGuard],
component: AuthorNavComponent,
children: [
{
path: '',
component: AuthorDashboardComponent,
pathMatch: 'full'
},
{
path: 'profile',
component: AuthorProfileComponent,
pathMatch: 'full'
},
{
path: 'journals/:journal_Id',
component: JournalPublicPageComponent,
pathMatch: 'full',
children: [
{
path: 'draftArticles', children: [
{
path: ':articleId',
component: DraftArticleComponent,
pathMatch: 'full'
},
{
path: '',
component: DraftArticlesComponent,
pathMatch: 'full'
}
]
},
{
path: 'articles', children: [
{
path: '',
component: ArticlesComponent,
pathMatch: 'full'
},
{
path: ':articleId',
component: ArticleComponent,
pathMatch: 'full'
}
]
}
]
}
Above is the structure my routes.以上是我的路线结构。
AuthorNavComponent
is my Sidenav within which I am also using another router-outlet for showing children routes. AuthorNavComponent
是我的 Sidenav,我还在其中使用另一个路由器插座来显示子路由。 The button that I am clicking is inside the Sidenav which ultimately calls a method in serviceA which will, after making a call to server, redirect to the above specified URL ie https://localhost:4200/author/journals/:journal_Id/draftArticles/:articleId我单击的按钮位于 Sidenav 内部,它最终调用 serviceA 中的一个方法,在调用服务器后,该方法将重定向到上面指定的 URL,即https://localhost:4200/author/journals/:journal_Id/draftArticles /:文章编号
Please let me know if any more information is required如果需要更多信息,请告诉我
Update更新
I tried using service in the middle named journalService
and created a BehaviorSubject<any[]>
to pass in the route of to navigate to.我尝试在名为journalService
的中间使用服务,并创建了一个BehaviorSubject<any[]>
来传递要导航到的路由。 In the component whose route is author/journals/3
, I subscribed to this BehaviorSubject<any[]>
and navigated upon changing the value of this BehaviorSubject passing along relative ActivatedRoute as follows:在路由为author/journals/3
的组件中,我订阅了此BehaviorSubject<any[]>
并在更改此 BehaviorSubject 的值时导航,并沿相对 ActivatedRoute 传递,如下所示:
Journal Component日志组件
this.js.navigateAgain.subscribe(data => {
if (data) {
this.router.navigate(data, {relativeTo: this.activatedRoute});
}
});
Other Component From where I am changing the value of BehaviorSubject<any[]>
其他组件我从那里更改BehaviorSubject<any[]>
的值
this.js.navigateAgain.next(['draftArticles', articleId]);
Update 2 Route could also be generated using this -> this.router.navigate(['author/journals', journalId, 'draftArticles', articleId]);
更新 2路由也可以使用此生成 -> this.router.navigate(['author/journals', journalId, 'draftArticles', articleId]);
but still the below error persists:(但仍然存在以下错误:(
Above technique generated the URL perfectly.以上技术完美地生成了 URL。 But I still get the following error:但我仍然收到以下错误:
It seems like, there is something wrong with the routing described far above.看起来,上面描述的路由有问题。
Answer to the above question is partially mentioned within the question at:上述问题的答案在以下问题中部分提及:
Update 2 Route could also be generated using this -> this.router.navigate(['author/journals', journalId, 'draftArticles', articleId]);更新 2 路由也可以使用此生成 -> this.router.navigate(['author/journals', journalId, 'draftArticles', articleId]); but still the below error persists但仍然存在以下错误
Using this, we can pass two parameters.使用它,我们可以传递两个参数。 However, I used router-outlet
for displaying child component.但是,我使用router-outlet
来显示子组件。 Such that, parent is shown in the main router-outlet while the children are shown in the subsequent router-outlets mentioned within the HTML templates of parent.这样,父级显示在主路由器出口中,而子级显示在父级的 HTML 模板中提到的后续路由器出口中。 The routing hierarchy defined above is like: author (parent) then journals/:journal_Id (child to author, parent to onward children) then draftArticles/:articleId (child to journals/:journal_Id) which will be shown in its parent for which I had to mention router-outlet tag in journals/:journal_Id component.上面定义的路由层次结构是这样的:作者(父级)然后是 journals/:journal_Id(子级到作者,父级到后续子级)然后 draftArticles/:articleId(子级到 journals/:journal_Id)将显示在我的父级中不得不在 journals/:journal_Id 组件中提及 router-outlet 标签。
Also, I learned that pathMatch: 'full'
should not be mentioned in children routes.另外,我了解到pathMatch: 'full'
不应在子路由中提及。 Now that parent component will also be showing along with the child component, I could simply if-else statement.现在父组件也将与子组件一起显示,我可以简单地使用 if-else 语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.