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

this.setData报错?

) {  },  /**   * 用户点击右上角分享   */  onShareAppMessage: function () {  }})// pages/load/load.js

// pages/load/load.js

Page({

  data: {

    userInfo: {},

    hasUserInfo: false,

    canIUseGetUserProfile: false,

  },

  onLoad() {

    //加载等待

    wx.showLoading({

      title: '加载中',

    })


    wx.login({

      success (res) {

        debugger

        if (res.code{

          //发起网络请求

          // wx.request({

          //   url: 'https://example.com/onLogin',

          //   data: {

          //     code: res.code

          //   }

          // })

          

          //授权

          if (wx.getUserProfile{

            this.setData({

              canIUseGetUserProfile: true

            })

          }


        } else {

          console.log('登录失败!' + res.errMsg)

        }

      }

    })


    

   //延时消失

    setTimeout(function(){

      wx.hideLoading()

    },2000)


  },

  getUserProfile(e) {

    // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认

    // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗

    wx.getUserProfile({

      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写

      success: (res) => {

        console.log(res.userInfo),

        this.setData({

          userInfo: res.userInfo,

          hasUserInfo: true

        })

      }

    })

  },

  getUserInfo(e) {

    // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息

    this.setData({

      userInfo: e.detail.userInfo,

      hasUserInfo: true

    })

  },

  

  /**

   * 生命周期函数--监听页面初次渲染完成

   */

  onReady: function () {


  },


  /**

   * 生命周期函数--监听页面显示

   */

  onShow: function () {


  },


  /**

   * 生命周期函数--监听页面隐藏

   */

  onHide: function () {


  },


  /**

   * 生命周期函数--监听页面卸载

   */

  onUnload: function () {


  },


  /**

   * 页面相关事件处理函数--监听用户下拉动作

   */

  onPullDownRefresh: function () {


  },


  /**

   * 页面上拉触底事件的处理函数

   */

  onReachBottom: function () {


  },


  /**

   * 用户点击右上角分享

   */

  onShareAppMessage: function () {


  }

})


网友回复

游戏结束べ:

this指向的对象发生了改变

微喵网络:
onLoad(){
  let that=this
  ...
  that.setData({
    ...
  })
  ...
}
没事:

在success外面,先var that = this,后面改成that.setData({})

col:

  success: (res) => {

写成这样就行 直接用this

Lv: Max Human:

wx.login的 success改为箭头函数就好了。

评论 抢沙发

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

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

云免签H5支付