【问题标题】:Ionic Framework Typescript: Callback function is not able to access `this` variables [duplicate]Ionic Framework Typescript:回调函数无法访问`this`变量[重复]
【发布时间】:2018-05-05 12:11:19
【问题描述】:

回调函数无法访问this 变量。

具体来说,我正在使用 d3.request 和 ionic 3。d3.request 能够进行休息调用,但我无法将响应分配给我的 this.data 变量。我可以 console.log(response)

如何访问 this.variables?

d3Request.request('http://47.184.52.10:8000/xyplot/')
  .header('Content-Type', 'application/json' )
  .post(JSON.stringify({'envelope':this.envelope,'chartTime': this.chartTime}),
      function(d){
          console.log(d.response);
          console.log(this.data) /*<---this.data*/
      })

带来错误'无法读取未定义的属性'数据''

如何解决此问题,以便将我的 d3.request 响应分配给 this.variable??

【问题讨论】:

    标签: javascript angular d3.js ionic-framework ionic3


    【解决方案1】:

    this 未在函数内部定义。使用箭头函数来解决这个问题

    d3Request.request('http://47.184.52.10:8000/xyplot/')
      .header('Content-Type', 'application/json' )
      .post(JSON.stringify({'envelope':this.envelope,'chartTime': this.chartTime}),
          d => {
              console.log(d.response);
              console.log(this.data) /*<---this.data*/
          })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-24
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-12
      • 1970-01-01
      • 2019-01-05
      相关资源
      最近更新 更多