有沒有開始從可觀察到的發光有條件值的方法?在我的情況下,.asObservable()
函數的用戶只能在CurrentUser
初始化後獲得第一個值。可觀察 - 開始發出有條件
export class CurrentUser {
private currentUser$: Observable<CurrentUser>;
private currentUserBehaviorSubject: BehaviorSubject<CurrentUser>;
public name: string = "";
constructor() {
this.currentUserBehaviorSubject = new BehaviorSubject(this);
this.currentUser$ = this.currentUserBehaviorSubject.asObservable();
}
public asObservable(): Observable<CurrentUser> {
//
if(user.name.length > 0){
return this.currentUser$;
}
else {
// ?????
}
}
public initialize(string name){
this.name = name;
this.currentUserBehaviorSubject.next(this);
}
}
export class SampleComponent {
constructor(
currentUser: CurrentUser
) {
currentUser.asObservable().subscribe(
(u: CurrentUser) => {
// i only want an INITIALIZED user here
},
error => {},
() => { }
);
}
}
誰初始化示例組件 –