lifecycle.js 864 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6
import {
  callPageHook
} from 'uni-core/service/plugins/util'

import {
  lifecycleMixin
fxy060608's avatar
init v3  
fxy060608 已提交
7
}
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  from 'uni-core/service/plugins/lifecycle'

export function initLifecycle (Vue) {
  lifecycleMixin(Vue)

  Vue.mixin({
    beforeCreate () {
      if (this.mpType === 'page') {
        this.$scope = this.$options.pageInstance
        this.$scope.$vm = this
        delete this.$options.pageInstance
      }
    },
    created () {
      if (this.mpType === 'page') {
        callPageHook(this.$scope, 'onLoad', this.$options.pageQuery)
        callPageHook(this.$scope, 'onShow')
      }
    },
    beforeDestroy () {
      if (this.mpType === 'page') {
        callPageHook(this.$scope, 'onUnload')
      }
    },
    mounted () {
      if (this.mpType === 'page') {
        callPageHook(this.$scope, 'onReady')
      }
    }
  })
}