zoukankan      html  css  js  c++  java
  • 微信小程序Component组件调用回调函数this指向不是本页面

    在微信小程序中如果在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())
        },
    
      },
    });
  • 相关阅读:
    git简单使用
    简单Spring和mybatis整合配置文件
    ASP.NET程序开发范例宝典
    C# DataSet和DataTable详解
    AOP
    匿名内部类
    数据库事务
    mybatis
    线程池
    单例模式
  • 原文地址:https://www.cnblogs.com/btxlc/p/11451712.html
Copyright © 2011-2022 走看看