我有route.params和route.queryParams的下一个网址
.../search/VALUE1?method=VALUE2我创造了一条路线
{
path : 'search/:id',
component: MyComponent
}组件
export class MyComponent {
...
constructor(private route: ActivatedRoute) {}
ngOnInit() {
combineLatest(this.route.queryParams, this.route.params)
.subscribe([queryParams, params] => {
console.log(ID:' + params('id') + ',M:' +queryParams('method'));
// code
})
}但是存在一个问题-当用户同时更改主param和query param时,订阅将被调用两次,例如用户调用。
.../search/VALUE1?method=VALUE2
.../search/VALUE3?method=VALUE4控制台日志
ID:VALUE1,M:VALUE2
ID:VALUE3,M:VALUE2 <-- redundant method call
ID:VALUE3,M:VALUE4如何同时订阅route.params、route.queryParams而只调用一次?
什么尝试了
private method: string;
//
ngOnInit() {
this.route.queryParams.subscribe(
queryParams => {
this.method = queryParams['method'];
});
this.route.params.subscribe(
params => {
console.log(ID:' + params('id') + ',M:' + this.method);
// code
}
}这件事做得很好。但是,我能确保this.method在订阅时包含正确的值吗?
发布于 2018-11-13 10:20:53
如果你想获得联谊会,你不需要订阅。当两者都改变的时候,下面的那个很好用。
ngOnInit() {
this.route.queryParams.subscribe( p => {
console.log("ID :" + this.route.snapshot.params['id'] + ',M:' + p['method'],)
})
}https://stackoverflow.com/questions/53278156
复制相似问题