将其赋值为Angular2中的值后,返回未定义的值

时间:2017-05-09 09:46:51

标签: json angular typescript

我是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??
  }
}

0 个答案:

没有答案