Angular2 异步设置用户?

Angular2 setting a user asynchronously?

我正在尝试提取用户信息并将其设置为我的用户变量,如下所示。我正在使用 angular-fullstack 样板。

this.getCurrentUser = this.Auth.getCurrentUser;
this.getCurrentUser(function(data){
    console.log('data: ', data); 
    this.username = data.name;
});

但是这样做的时候,我得到一个错误,说 this.username 没有定义。因此,我的问题是如何将相关信息放入我的变量中。

尝试使用 arrow function expression 作为回调,因为

Arrow functions capture the this value of the enclosing context

this.getCurrentUser = this.Auth.getCurrentUser;
this.getCurrentUser(data => {
    console.log('data: ', data); 
    this.username = data.name;
});