page-parser.js 1.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import Vue from 'vue'

import {
  initData,
  initHooks,
  handleEvent,
  initBehaviors,
  initVueComponent,
  PAGE_EVENT_HOOKS
} from 'uni-wrapper/util'

import {
  handleRef,
  handleLink,
fxy060608's avatar
fxy060608 已提交
15 16
  initBehavior,
  initChildVues,
fxy060608's avatar
fxy060608 已提交
17
  initSpecialMethods
fxy060608's avatar
fxy060608 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
} from './util'

const hooks = [
  'onShow',
  'onHide',
  // mp-alipay 特有
  'onTitleClick',
  'onOptionMenuClick',
  'onPopMenuClick',
  'onPullIntercept'
]

hooks.push(...PAGE_EVENT_HOOKS)

export default function parsePage (vuePageOptions) {
  let [VueComponent, vueOptions] = initVueComponent(Vue, vuePageOptions)

  const pageOptions = {
    mixins: initBehaviors(vueOptions, initBehavior),
    data: initData(vueOptions, Vue.prototype),
    onLoad (args) {
      const properties = this.props

      const options = {
        mpType: 'page',
        mpInstance: this,
        propsData: properties
      }

      // 初始化 vue 实例
      this.$vm = new VueComponent(options)
fxy060608's avatar
fxy060608 已提交
49 50

      initSpecialMethods(this)
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55 56 57 58

      // 触发首次 setData
      this.$vm.$mount()

      this.$vm.$mp.query = args // 兼容 mpvue
      this.$vm.__call_hook('onLoad', args)
    },
    onReady () {
fxy060608's avatar
fxy060608 已提交
59
      initChildVues(this)
fxy060608's avatar
fxy060608 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72
      this.$vm._isMounted = true
      this.$vm.__call_hook('mounted')
      this.$vm.__call_hook('onReady')
    },
    onUnload () {
      this.$vm.__call_hook('onUnload')
      this.$vm.$destroy()
    },
    __r: handleRef,
    __e: handleEvent,
    __l: handleLink
  }

fxy060608's avatar
fxy060608 已提交
73 74
  initHooks(pageOptions, hooks)

fxy060608's avatar
fxy060608 已提交
75 76
  return pageOptions
}