我是Angular2的新手。我正在尝试学习如何解析JSON。当我搜索这个时,注意到这个问题。请解释一下,为什么它在赋值后返回undefined JSON.stringfy()' s返回值Δα
import {Component} from '@angular/core'
import {User} from './model/user'
import {LoginService} from "./login.service";
@Component({
selector:'login-component',
templateUrl: './login.html',
providers:[LoginService]
})
export class LoginComponent{
constructor (private loginService:LoginService) { }
result:string;
goToMine(username:string,password:string){
this.loginService.checkUser(new User(username,password)).subscribe(
myData=> this.result=JSON.stringify(myData),
error=> alert(error),
()=> console.log("Finished")
);
console.log(this.result);//why does it print undefined??
}
}