码迷,mamicode.com
首页 > 微信 > 详细

微信小程序Component组件调用回调函数this指向不是本页面

时间:2019-09-03 11:33:48      阅读:413      评论:0      收藏:0      [点我收藏+]

标签:页面   time   函数   eth   bsp   console   解决办法   程序   llb   

在微信小程序中如果在Component中写回调函数, 那么this的指向是undefined

 

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {

    getData(callback) {

          if (callback) {
            callback();
          }

    },
    funcA () {
        console.log(this)  //这里的this不是指向Component实例
    },

  
  },

  lifetimes: {
    attached: function() {
      this.getData(this.funcA)
    },

  },
});
       

 

 

解决办法: 在调用回调函数的时候使用箭头函数

 

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {

    getData(callback) {

          if (callback) {
            callback();
          }

    },
    funcA () {
        console.log(this)  //这里的this不是指向Component实例
    },

  
  },

  lifetimes: {
    attached: function() {
      this.getData(()=> this.funcA())
    },

  },
});

 

微信小程序Component组件调用回调函数this指向不是本页面

标签:页面   time   函数   eth   bsp   console   解决办法   程序   llb   

原文地址:https://www.cnblogs.com/btxlc/p/11451712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!