小程序组件生命周期

介绍微信小程序组件生命周期,包括pageLifetimes(show、hide、resize)和lifetimes(attached、detached),其中pageLifetimes在每次页面载入时都会执行,可用于初始化数据和响应页面状态变化。

作者:zhuge··预计阅读 13 分钟·615 阅读·0 评论
小程序组件生命周期

Component({

pageLifetimes: {

    show: function() {

      // 页面被展示  初始化数据的时候操作

      this._randColor()

    },

    hide: function() {

      // 页面被隐藏

    },

    resize: function(size) {

      // 页面尺寸变化

    }

  },

  lifetimes: {

    attached: function () {

      try {

       // 需要在初始化的时候做的操作

      } catch (e{

      }

      this._randColor()

      // 在组件实例进入页面节点树时执行

    },

    detached: function () {

      // 在组件实例被从页面节点树移除时执行

    },

  }

}

pageLifetimes在每次页面载入时都会执行

评论

加载中...