在ngOnInit()上,我正在从以下代码中读取查询参数。如何将可观察对象转换为字符串?
constructor(private router: ActivatedRoute) { }
textToDisplay: Observable<string>;
ngOnInit() {
this.textToDisplay = this.router
.queryParamMap
.pipe(map(params => params.get('session_id') || 'None'));
}
答案 0 :(得分:1)
使用订阅方法。看下面的例子。
this.route.queryParams.subscribe(params => {
console.log(params); // {order: "popular"}
this.order = params.order;
console.log(this.order); // popular
});
答案 1 :(得分:1)
您可以尝试以下解决方案:
constructor(private router: ActivatedRoute) { }
textToDisplay: Observable<string>;
ngOnInit() {
this.router.queryParams.subscribe(resp => {
if (resp.status) {
//get response data
this.textToDisplay = resp
}
});
}