public 属性 的上下文在 Typescript 的 Map 运算符中未定义

this context of public property is Undefined inside Map operator in Typescript

我正在执行如下所示的代码:

isValidLogin():Observable<boolean> {
    return this.http.get(this._checkLoginUrl)
       .map(res=>res.json())
        .map((res) => {
            if (res.success) {
                this.loggedIn = true;
            }
            return res;
        });

}

已转译的 Js:

  LoginService.prototype.isValidLogin = function () {
    var _this = this;
    return this.http.get(this._checkLoginUrl)
        .map(function (res) { return res.json(); })
        .map(function (res) {
        if (res.success) {
            _this.loggedIn = true;
        }
        return res;
    });
};

这里的loggedIn是class的public属性,但是显示undefinedthis这里变成了Map对象上下文,它无法为 loggedIn.

赋值

我认为 lambda 表达式应该能够访问 class 对象的上下文。 是不是我做错了什么?

I think the lambda expression should be able to access this context of class object. Is there something I have done wrong here

class 成员可能也需要一个箭头。而不是:

isValidLogin():Observable<boolean> {

写入:

isValidLogin = ():Observable<boolean> => {

更多

https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html