行为主体初始值是否为null? [英] Behaviour subject initial value null?

查看:57
本文介绍了行为主体初始值是否为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private customer: Subject<Object> = new BehaviorSubject<Object>(null);

setCustomer(id, accountClassCode) {
    this.customer.next({'id': id, 'accountClassCode': accountClassCode});
}

getCustomer() {
    return this.customer.asObservable();
}

我正在使用这部分代码,但遇到一个错误,找不到id为null的错误.有什么解决方案可以获取不为空的初始值?

I'm using this part of code but I'm getting an error that can not find id of null. Is there any solution to get initial value that is not null?

推荐答案

BehaviorSubject的目的是提供初始值.它可以是null或其他任何内容.如果无法提供有效的初始值(当尚不知道用户ID时),则不应使用它.

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)提供类似的行为(在订阅时发出最后一个值),但是只有在使用next进行设置后才具有初始值.

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

可能应该是

private customer: Subject<Object> = new ReplaySubject<Object>(1);

这篇关于行为主体初始值是否为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆