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

查看:21
本文介绍了行为主体初始值 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天全站免登陆