html52.com
我爱小程序-开发者交流社区

请教setData()的用法

问题模块
API和组件

项目要求:

点击页面上的“拍照”按钮拍摄一张照片,然后将照片展示在页面上。



代      码:

<button bindtap="chooseImage">拍照</button>
<image src="{{src}}"></image>


Page({
  data: {
    src: ""
  },
  chooseImage: function () {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePath = res.tempFilePaths
        this.setData({
          src: tempFilePath[0]
        });
      }
    });
  }
});



问   题:

在开发工具中测试,点击“拍照”按钮,可以正常选择本机中的图片。

但图片选中后,<image>组件没有反应,并不展示选中的图片。查看开发工具的“调试”,显示:


WAService.js:3 thirdScriptError
this.setData is not a function;at pages/test1/test1 chooseImage function;at api chooseImage success callback function
TypeError: this.setData is not a function

………


求教!!!

网友回复

陈浠:

:)


谢了

夜风来袭:

哪有书啊,就看微信官方的doc呗哪有书啊,就看微信官方的doc呗陈浠:多谢。问题解决。初学小白,对this的了解极少。不情之请:就这个问题,能否推荐一下应该多看一下哪方面的书籍?


陈浠:

多谢。问题解决。


初学小白,对this的了解极少。不情之请:就这个问题,能否推荐一下应该多看一下哪方面的书籍?

¼:

在网络请求之前定义 var that =this;

在处理网络返回数据时,不能直接使用this。 要事先定义一个this的代理。

夜风来袭:

 var that = this;放到function内第一行

陈浠:

………


sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePath = res.tempFilePaths;
        var that = this;
        that.setData({
          src: tempFilePath[0]
        });
      } 


………


依旧错误提示

WAService.js:3 thirdScriptError
that.setData is not a function;at pages/test1/test1 chooseImage function;at api chooseImage success callback function
TypeError: that.setData is not a function

¼:

var that =this;

that.setData({

....

});

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

真正的个人免签约支付接口

云免签H5支付