有关setdata问题?

小程序 文章 2020-08-15 16:01 0 145 全屏看文

有关setdata问题?Questions about setdata?

有关setdata问题?
console.log("befor:" + that.shareImg);that.setData({ shareImg: res.tempFilePath });console.log("after:" + that.shareImg);console.log(res.tempFilePath);输出如下:befor:undefinedafter:undefinedhttp://tmp/wxb4f060XXXXXX7.pngundefined

那个shareimg我还有赋初始值,然而一样是未定义?不懂

js里面怎么才能用全局变量,困扰了一周,求救

console.log ("befor:" +  that.shareImg ); that.setData ({ shareImg:  res.tempFilePath  }); console.log ("after:" +  that.shareImg ); console.log ( res.tempFilePath )The output is as follows: befor:undefinedafter :unde finedhttp://tmp/wxb4f060XXXXXX7.pngundefined The shareimg I have assigned initial value, but the same is undefined? I don't know how to use global variables in JS. I've been bothering for a week. Please help me

回答:

田迪:
that.data.shareImg


给糖🍭就不闹:
console.log("before:" + that.data.shareImg);console.log("after:" + that.data.shareImg);


ash:谢谢,已经明白
Distance:

什么意思?你要获取data中的数据是that.data.shareImg

ash:res.tempFilePath 需要存放到全局变量shareImg,但是你看打印的结果明显没存进去,就是这一个意思,可能是我的js语法错了?还是其他问题导致
Distance:首先你这个不叫全局变量,setData的是page数据绑定的变量,你要获取page中data的数据就是that.data.shareImg 懂?
全局变量在app.js中,如果你放在globalData那就是 getApp().globalData.shareImg = res.tempFilePath 获取也是getApp().globalData.shareImg
ash:我明白了,谢谢,第一次写js有点绕不过弯

-EOF-