2017-06-14 9 views
0

現在我嘗試使用observables。用戶可能會註銷並以其他用戶身份登錄,因此非常適合流式傳輸。但我如何和在哪裏實施我的this.loadingService.showLoading();this.loadingService.hideLoading();如何顯示加載直到下一個項目或錯誤到達observables?

它不能在subscription,因爲這永遠不會完成。而this.loginService.login(credentials)也給回user$ -stream

public user$: Observable<any> = this.loginService.user$; 

this.subscription.add(this.user$.subscribe(value => { 
    this.user = value; 
}, err => { 
    console.error('error in user stream', err); 
},() => { 
    console.error('impossible') 
})); 

userLoginHandle(credentials) { 
    this.loginService.login(credentials) 
} 
+0

我很明白你的問題爲什麼不只是簡單地說:'this.loadingService.showLoading();這個用戶流中的錯誤('error in user stream('error in user stream')'); this.user $ .subscribe(this.user $ .subscribe(value => { this.user = value; this.loadingService.hideLoading(); },err => { console.error ',err); this.loadingService.hideLoading(); },()=> { console.error('impossible') }));' –

+0

如果沒有'loginService.login() '沒有被調用,然後'hideLoading()'是不可能的,因爲'showLoading()'沒有被調用。用戶可以到達 – mnewmedia

+0

不用登錄嗎? –

回答

2

這個是什麼?

this.subscription.add(this.user$.subscribe(value => { 
    this.loggingIn = false; 
    this.user = value; 
}, err => { 
    this.loggingIn = false; 
    console.error('error in user stream', err); 
},() => { 
    console.error('impossible') 
})); 

userLoginHandle(credentials) { 
    this.loggingIn = true; 
    this.loginService.login(credentials) 
} 
+0

是的,這是一個可行的解決方案,但使用承諾,感覺更清潔,代碼更少。當然有辦法完成它,但我真正想要的是一個乾淨的解決方案。但從來沒有想過我決定不在任何地方使用'observables',只在我認爲有意義的地方使用。 – mnewmedia

0

你可以實現一個setter用戶對象

set user(value) { 
    if(value) { 
    this.loadingService.hideLoading(); 
    } 
} 
相關問題