zoukankan      html  css  js  c++  java
  • 微信小程序 this.setData is not a function

      在一般的函数中:

    1 bindFaChange1: function (e) {
    2     console.log('picker发送选择改变,携带值为', e.detail.value)
    3     this.setData({
    4       index1: e.detail.value
    5     })
    6   }

      this.setData是正确的。

      但当在函数中有个请求(wx.request)时:

     1 formSubmit: function (e) {
     2     wx.request({
     3       method: 'POST',
     4       header: header,
     5       url: url,
     6       dataType: 'json',
     7      success: function (res) {
     8            this.setData({
     9               data1: true
    10             })
    11       }
    12     })
    13 }

      或者执行定时任务时候:

     1 var si = setInterval(function () {
     2         that.setData({
     3           sendVerifyingCodeText: curCount + '秒后重新获取'
     4         });
     5         that.setData({
     6           sendSmsCodeDisable: true
     7         });
     8         curCount--;
     9         if (curCount <= 0) {
    10           that.setData({
    11             sendSmsCodeDisable: false
    12           }),
    13           clearInterval(si);
    14         }
    15       }, 1000);

      这样会报错误:this.setData is not a function.

      这个在新的函数内的this代表的是这个函数体,所有是没有this.setData。这个类似java中的this指的是当前对象,但是javascript是以函数为主体的,所以就是this在函数内部就当前函数。修改未:

      解决方法就是 :在请求(wx.request)或者新的非当前js的方法外面添加:var that=this;然后用:

    1 that.setData({
    2               data1: true
    3             })
  • 相关阅读:
    恐怖如斯
    java在vscode中配置环境的坑
    python的迭代器模块
    一个模仿输入print就有这么多知识点
    30个python常用小技巧
    第一个只出现一次的字符
    UIScrollView属性
    iOS 中UISlider常用知识点
    iOS中UISegmentedControl常用属性
    iOS触摸事件
  • 原文地址:https://www.cnblogs.com/lcngu/p/8329490.html
Copyright © 2011-2022 走看看