【发布时间】:2017-04-03 14:41:16
【问题描述】:
我已经设置了以下路由系统
export const MyRoutes: Routes = [
{path: '', redirectTo: 'new', pathMatch: 'full'},
{path: ':type', component: MyComponent}
];
并具有以下导航系统
goToPage('new');
goToPageNo('new', 2);
goToPage(type) {
this.router.navigate([type]);
}
goToPageNo(type, pageNo) {
this.router.navigate([type], {queryParams: {page: pageNo}});
}
示例网址如下所示
http://localhost:3000/新
http://localhost:3000/new?page=2
http://localhost:3000/更新
http://localhost:3000/updated?page=5
有时他们有可选 queryParams(页面)
现在我需要同时阅读 route params 和 queryParams
ngOnInit(): void {
this.paramsSubscription = this.route.params.subscribe((param: any) => {
this.type = param['type'];
this.querySubscription = this.route.queryParams.subscribe((queryParam: any) => {
this.page = queryParam['page'];
if (this.page)
this.goToPageNo(this.type, this.page);
else
this.goToPage(this.type);
})
})
}
ngOnDestroy(): void {
this.paramsSubscription.unsubscribe();
this.querySubscription.unsubscribe();
}
现在这没有按预期工作,访问没有 queryParams 的页面可以工作,然后我访问一个带有 queryParams 的页面“goToPageNo”被多次调用,因为我在路由参数中订阅 queryParams。
我查看了 Angular 2 文档,他们没有任何示例或代码同时实现对 路由参数和 queryParams 的订阅。
有什么方法可以正确地做到这一点吗?有什么建议吗?
【问题讨论】: