Behaviour subject initial value null?

后端 未结 3 1761
Happy的楠姐
Happy的楠姐 2020-12-03 04:15
private customer: Subject = new BehaviorSubject(null);

setCustomer(id, accountClassCode) {
    this.customer.next({\'id\': id, \'account         


        
      
      
      
3条回答
  •  生来不讨喜
    2020-12-03 04:54

    The purpose of BehaviorSubject is to provide initial value. It can be null or anything else. If no valid initial value can be provided (when user id isn't known yet), it shouldn't be used.

    ReplaySubject(1) provides a similar behaviour (emits last value on subscription) but doesn't have initial value until it is set with next.

    It likely should be

    private customer: Subject = new ReplaySubject(1);
    
        

    提交回复
    热议问题