收藏
回答

有关setdata问题?

console.log("befor:" + that.shareImg);
that.setData({ shareImg: res.tempFilePath });
console.log("after:" + that.shareImg);
console.log(res.tempFilePath);
输出如下:
befor:undefined
after:undefined
http://tmp/wxb4f060XXXXXX7.png
undefined

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

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

回答关注问题邀请回答
收藏

3 个回答

  • brave
    brave
    2020-08-15

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

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


    2020-08-15
    有用
    回复
  • 给糖🍭就不闹
    给糖🍭就不闹
    2020-08-15
    console.log("before:" + that.data.shareImg);
    console.log("after:" + that.data.shareImg);
    


    2020-08-15
    有用
    回复 1
    • ash
      ash
      2020-08-15
      谢谢,已经明白
      2020-08-15
      回复
登录 后发表内容
问题标签