激活的路由可选参数,可观察到发出每个字符而不是整个字符串

时间:2019-04-05 19:12:29

标签: angular angular-activatedroute

我有一个方法,其内容为:

this.route.paramMap.pipe(
    switchMap((params: ParamMap) => {       
    let fooValue = params.get('selectedid');
    console.log("inside switch map with value as " + fooValue);
    return fooValue;
  })
)

此Observable分配为类型Observable<string>. 现在,当我在上面订阅时,如果selectedid = ABCD,那么我将对A,B,C,D分别进行4次next()方法的调用,这是什么原因?


这仅发生在switchMap上,而不发生在地图操作符上。

当我使用this.router.navigate(['/mypath', path.id]);并与之一起使用switchmap时,它不会分割字符串。它以this.router.navigate(['/mypath1', { selectedid: obj.id, foo: "foo" }]);语法

发生

1 个答案:

答案 0 :(得分:0)

尝试

this.route.paramMap.subscribe(params => {
    this.id = params.get("selectedid")
    alert(this.id)
})