我有一个 url ,我想同时获取 params 和 queryparams 并且每个请求参数都应该使用 '&' 分开,第一个除外。第一个将由 '?' 分隔对于前 请求网址:abc.com/abc:abc:abc:0xa35df9cdf?ab=1234567?lang=en 我应该进入变量 let result = abc:abc:abc:0xa35df9cdf?ab=1234567&lang=en ,我用过这个,但我需要一个单一的变量,也是动态的,我的查询参数可以是多个
import { Component,OnInit,ActivatedRoute } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
constructor(route: ActivatedRoute) {}
ngOnInit() {
this.route.params.subscribe((param: any) => { console.log(param) })
this.route.queryParams.subscribe((param: any) => { console.log(param) })
}
}
答案 0 :(得分:0)
以后要更清楚你想做什么,你必须使用某种http get函数并先获取url,然后将结果一个一个地通过管道传输到最后。这允许您分离变量。
string1 ?: string;
string2 ?: string;
this.httpClient.get(getRoute).pipe(map(c => {
c = c,
//convert to unknown because we don't know how the format will be helps //with enum values
this.string1=c?.abc as unknown as string,
this.string2=c?.xyx as unknown as string
publishReplay(1), // this tells Rx to cache the latest emitted
refCount();
}
)
).subscribe((d) => {
const url = yoururl + '&' + this.string1 + '&' this.string2
this.httpClient.post(url,{}).subscribe((results)=>{
console.log(results)
})
// removed the routes interface, not relevant
});