订阅值更改网址角度

时间:2020-04-15 11:46:53

标签: angular

我有这样的网址

www.test.com?state=da#value=1&test=2

当我想订阅网址更改时,我已经尝试过这样

this.route.params.subscribe(() => {
      this.urlParams = this.route.snapshot.queryParamMap.get('state');
      this.urlFragment = this.route.snapshot.fragment;
      if (!this.urlParams) {
        this.router.navigate([`/error/401`]);
      }
});

这很好,因为我可以检查是否在 state 参数和值中进行了更改,但是是否在 value 中进行了更改,例如 value = 2 网址片段未更改?

我知道我会喜欢的

this.route.fragment.subscribe();

这样,我有两个订阅,我想订阅url的任何更改?有办法吗?

1 个答案:

答案 0 :(得分:0)

您可以在订阅中使用 rxjs 中的 combineLatest ,如下所示:

import { combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
...
combineLatest(this.route.params, this.route.fragment).pipe(
  map(([params, fragment]) => console.log(params + '' + fragment) )
).subscribe()
相关问题