我的网站

TypeError: Cannot read property 'setData' of undefined

分类:其他 发布日期:2022-07-18 16:51 浏览:2
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 api获取文章缩略图,特色图片Featured Image 下一篇:一种Git冲突:commit your changes or stash them before you can merge 解决方法 →

相关文章