行为主体在值改变时具有不同的发射计数

问题描述 投票:0回答:1

我们在某些服务中有一个行为主题

  city = new BehaviorSubject(config.defaultCity);

  get cityObservable(){
    return this.city.asObservable();
  }

当我们在某些组件中获得可观察值时,我们可以侦听值的变化,因此我们基于城市获取不同的数据。

喜欢这样


  categories: Observable<any>;
  activeCoupons: Observable<any>;

  ngOnInit() {

    this.categories = this.utilsService.cityObservable
    .pipe(
      debounceTime(100),
      switchMap(cityId => this.categoryService.fetchCategories(cityId)),
      map((response: any) => response.data)
    );


    this.activeCoupons = this.utilsService.cityObservable
    .pipe(
      debounceTime(100),
      switchMap(cityId => this.couponService.fetchActiveCoupons(cityId))
    );


  }

问题在于,当城市值更改时,fetchCategories被调用一次,而当城市值更改时,fetchActiveCoupons被调用了两次!为什么它的价值两次发出?他们没有相同的代码吗?

angular rxjs6
1个回答
1
投票
它被调用两次,因为它有两个订阅者。
© www.soinside.com 2019 - 2024. All rights reserved.