4
假設我有一個Angular 2服務。 createToken
函數獲取登錄令牌從服務器:訂閱RxJS Observable兩次。獲得兩次響應的價值
//ANGULAR 2 SERVICE:
//login to application
createToken(email: string, password: string) {
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
});
let data = 'username=' + email + '&password=' + password + '&grant_type=password';
this.http.post(this.loginURL, data, { headers: headers })
.map(response => response.json())
.subscribe(
response => {
localStorage.setItem(this.storageTokenKey, response.access_token);
console.log("Token: " + response.access_token);
},
error => {
console.log(error.text());
});
}
我需要做兩件事情與我的令牌:
- 將其保存到本地存儲(這將是完成每一次,所以它應該是在服務)
- 做一些操作上的成分(其顯示或其他)
爲了做到這一點(因爲據我所知觀測量),我createToken
函數必須返回Observable<>
以允許將使用它的組件調用其.subscribe
方法對結果執行一些操作。
不幸的是,as I see from RxJS documentation,.subscribe
方法不返回Observable<>
我應該怎麼做,然後才能夠調用.subscribe()
方法兩次?
這是錢。謝謝! – Aarmora