0
我有這樣的服務:如何將響應從服務傳遞到組件?
Login (body): Observable<Login[]> {
//let bodyString = JSON.stringify(body); // Stringify payload
var bodyString = 'email='+body.email +'&password='+body.password;
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded'});
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http.post('/logiranje', bodyString, options) // ...using post request
.map(response => {return response}) // ...and calling .json() on the response to return data
.catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any
}
我有分量:
submitLogin(values){
var current = this;
// Variable to hold a reference of addComment/updateComment
let loginOperation:Observable<any>;
loginOperation = this.loginService.Login(values);
loginOperation.subscribe(
(response) => { console.log("Success Response" + response)},
function(error) { console.log("Error happened" + error)},
function(){
current.router.navigate(['/home']);
console.log("the subscription is completed");
}
);
}
我要的是檢查:
if(response.isLoggedIn){
current.router.navigate(['/home']);
}
但我不知道如何從服務傳遞價值零件? 任何建議我該怎麼做?
即時得到,當你用你的代碼什麼呢'的console.log(響應)'顯示this.data未定義 – uzhas
錯誤? 您是否像我在第一行中那樣定義了一個'data'變量? – YounesM
「成功ResponseResponse與狀態:200確定爲URL:http:// localhost:3000/logiranje」login.component.js:38:68 訂閱完成 – uzhas