箭头函数里面 this 为 undefined
发布于 4 年前 作者 yanggao 1310 次浏览 来自 问答

页面里面有个函数名为 add,如果以 add: function(result) {} 的形式定义,在函数里面可以用 this.data 拿到页面的数据,但如果以 add: result => {} 的形式定义函数,在函数里面 this  报 undefined,拿不到页面数据,我在箭头函数里面,调用 this  之前先把 this 赋值给 that,然后调用 that.data 也是 undefined,请问如果我要用箭头函数来写,这个应该怎么改呀

4 回复

小程序里面的箭头也就在调用wx.xxx的success之类的函数用起来比较输入一些

在option那种的function直接写城add(params){}就行了

onLoad: function (options) {    

    let result= ‘’

    this.add(this, result);

},

add: ( (that, result) => {

    console.log(“2222222222222222222”,that)

}),

你是要定义一个箭头函数形式的函数?

回到顶部