此值=未定义(TypeScript)

问题描述 投票:0回答:2

变量this.engenes_comparte以未定义的形式出现在预订记录的内部,但在外部起作用时显示在外部。

 baja(){    
  this._restService.getEngines(this._globalService.currentFisherMan.nid).subscribe((data : any[]) => {
    let state = 0;
        for (let index = 0; index <= data.length; index++) {
          for (let l = 0; l <= this.engines_compare.length; l++) {
            if(data[index].nid != this.engines_compare[l].nid){


            }
          }
        }
        console.log(this.engines_compare);
  });



  this.sube();
}
javascript arrays angular typescript this
2个回答
0
投票

我无法理解您的程序的详细信息,但是我想这是由于function(){ this }() => {this}中的区别)>

this在允许功能中与this样式功能中的function(){}具有不同的含义。

let A = {
   f() {
      return this.a // this is caller of f. This `this` is not always A. 
   }
   f2 = () => {
      return this.a // This `this` always equals A
   }
   a: 1
}
A.f() // => 1 because caller of f is A, so `this` is A.
A.f.call({}) // => undefined because caller of f is not A, but {}
A.f2() // => 2 
A.f2().call({}) // => 2 because this is always A

0
投票

您可以在订阅外部创建常量,即const _this = this;,然后可以在订阅内部使用_ this

© www.soinside.com 2019 - 2024. All rights reserved.