我希望在我的应用程序中包含以下路由:
export const routes: Routes = [
{
path: ':universityId',
component: UniversityComponent,
children: [
{ path: 'info', component: UniversityInfoComponent },
{ path: 'courses/:status', component: CoursesByStatusComponent }
]
}
]在/1/info或/1/courses/open url上,如何仅从UniversityComponent更改:universityId
简单的router.navigate(['../', 2], {relativeTo: currentRoute })不能这样做,因为它会重定向到/2,丢失所有其他信息。使用'../2/courses/open也不是一种选择--那时我可以在任何子路由上。我能想到的最好的办法是:
const urlTree = this.router.parseUrl(this.router.url);
urlTree.root.children['primary'].segments[0].path = '2';
this.router.navigateByUrl(urlTree);但这有点丑陋
发布于 2018-08-20 20:30:04
这非常简单。您可以尝试使用this.router.navigate("../2",“课程”,"open",{relativeTo: this.route});
(或)
this.router.navigate("../2",“课程/开放”,{relativeTo: this.route});
这会将您重定向到../2/courses/open。
发布于 2017-07-20 03:12:41
下面是我上面关于使用元组的评论中的一些代码:所以将这个模式复制两次……该shows如何将这些类集成到您的项目中。所以:
对于链接中的代码,请将data-component-resolver.ts更改为parent-child-resolver.ts。
下的parent-child.component.ts
parent-child-resolver.ts
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {Resolve, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Parent} from '../shared/model/parent';
import {Children} from '../shared/model/children';
import {ParentService} from '../providers/parent.service';
@Injectable()
export class parentChildResolver implements Resolve<[Parent,(Children[])>
constructor(private parentService : ParentService) {}
resolve( route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
) : Observable<[Parent,(Children[])>
{
return this.parentService.findParentById(route.params['id'])
.switchMap(parent =>
this.parentService.findChildrenForParent(parent.id),
(parent, children) => [parent, children]
);
}
}parent-child.component.ts
import {Component, OnInit} from '@angular/core';
import {Parent} from '../shared/model/parent';
import {Children} from '../shared/model/children';
import {Observable} from 'rxjs';
import {ActivatedRoute} from '@angular/router';
@Component({
selector: 'parent-child',
templateUrl: './parent-child.component.html'
})
export class ParentChildComponent implements OnInit{
$parent: Observable<Parent>;
$children: Observable<Children[]>;
constructor(private route:ActivatedRoute) {}
ngOnInit() {
this.parent$ = this.route.data.map(data => data['key'][0]); //from router.config.ts
this.children$ = this.route.data.map(data => data['key'][1]);
}
}https://stackoverflow.com/questions/40260738
复制相似问题