订阅Angular 2中的route params和queryParams
发布时间:2020-12-17 07:59:18 所属栏目:安全 来源:网络整理
导读:我已经设置了以下路由系统 export const MyRoutes: Routes = [ {path: '',redirectTo: 'new',pathMatch: 'full'},{path: ':type',component: MyComponent}]; 并有以下导航系统 goToPage('new');goToPageNo('new',2);goToPage(type) { this.router.navigate([
我已经设置了以下路由系统
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}}); } 示例网址如下所示
有时他们有可选的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”被多次调用,因为我订阅了route Params内的queryParams. 我查看了Angular 2文档,它们没有任何示例或代码,其中同时实现了对params和queryParams的订阅. 有什么方法可以做到这一点吗?有什么建议么?
我通过在订阅之前使用Observable.combineLatest组合observable来设法获得queryParams和Params的单一订阅.
例如. var obsComb = Observable.combineLatest(this.route.params,this.route.queryParams,(params,qparams) => ({ params,qparams })); obsComb.subscribe( ap => { console.log(ap.params['type']); console.log(ap.qparams['page']); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |