角度-结合参数和queryParams可观察对象 [英] Angular - Combine params and queryParams observables
本文介绍了角度-结合参数和queryParams可观察对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何将这些与可观察物结合在一起?它们具有相同的功能.
How can I combine these to observables into one? They have the same functionality.
this.sub = this.route.params.subscribe((params: any) => {
// functionality
});
this.sub = this.route.queryParams.subscribe((params: any) => {
// same functionality
});
推荐答案
您可以使用 CombineLatest 为此:
import { ActivatedRoute } from '@angular/router';
import { combineLatest } from 'rxjs';
// ...
class MyComponent {
constructor(private route: ActivatedRoute) { }
ngOnInit() {
const params = this.route.params;
const queryParams = this.route.queryParams;
combineLatest(params, queryParams, (params, qparams) => ({ params, qparams }))
.subscribe(allParams => console.log(allParams.params, allParams.qparams));
}
}
params和qparams将是具有属性名称作为参数名称和值作为值的对象.因此,对于一条路线:
params and qparams will be objects with the property name as the param name and the value as the value. So for a route:
RouterModule.forRoot([{
path: 'test1/:id',
component: Test1Component
}])
带有参数
http://localhost:4200/test1/paramvalue?qparam=qpvalue
所有参数将是
{
params: {
id: "paramvalue"
},
qparams: {
qparam: "qpvalue"
}
}
这篇关于角度-结合参数和queryParams可观察对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文