TypeError: Cannot read property ‘setData’ of undefined

2022-07-18
success: function (res) {
   this.setData({})
}

改成如下:

success: (res) =>  {
   this.setData({})
}

【原因分析】
为什么呢?
因为两种写法this指向不同。

success: (res) => {
        console.log("(res) => { }时:" + this);
      },

--------------

success: function (res){
        console.log("function (res)时:" + this);
       },

对比分析:

function (res)时:undefined
 (res) => { }时:[object Object]

function (res)写法时 ,thisundefined未定义的。
(res) => { }写法时thisObject

【分析总结】
1、如果函数作为对象的方法调用,this指向的是这个上级对象,即调用方法的对象。
2、如果是构造函数中的this,则this指向新创建的对象本身。

相关内容
最新

坚持的力量

wordpress建站,视频剪辑拍摄,动画制作