Angular2 Query Params Subscription Fires Twice

后端 未结 8 1486
闹比i
闹比i 2020-12-25 11:07

Trying to handle an OAuth login scenario where if the user lands on a page with authorization_code in the query string, we process the token and continue

8条回答
  •  悲&欢浪女
    2020-12-25 11:42

    I guess that's by design.

    queryParams is the BehaviorSubject

    As you can see in the docs

    One of the variants of Subjects is the BehaviorSubject, which has a notion of "the current value". It stores the latest value emitted to its consumers, and whenever a new Observer subscribes, it will immediately receive the "current value" from the BehaviorSubject.

    As workaround you can use debounceTime operator as follows:

    import 'rxjs/add/operator/debounceTime';
    
    this._route.queryParams
      .debounceTime(200)
      .subscribe(params => {
        console.log(params);
      });
    

提交回复
热议问题