【发布时间】:2017-12-02 12:17:22
【问题描述】:
每次 http auth API 返回授权用户时,我都需要设置currentUser: BehaviorSubject<>()。现在我正在使用do 将新用户发送到BehaviorSubject,但完成这样的任务似乎是一种肮脏的方式。
是否有 fork 方法或类似的方法可以更新观察者并返回原始的 observable?
我有什么
public currentUser: BehaviorSubject<any> = new BehaviorSubject<any>(null);
authUser(email: String, password: String) {
return this.http.post('api.com/auth', {
email: email,
password: password
})
.do(user => this.currentUser.next(user))
}
我想要什么
return this.http.post('api.com/auth', {
email: email,
password: password
})
.fork(this.currentUser)
【问题讨论】:
-
但更新
Subject的唯一方法是通过next()方法,不是吗 -
你需要使用
behaviorSubject为什么不能直接从http方法使用observable?