page-parser.js 2.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import Vue from 'vue'

3 4 5 6
import {
  stringifyQuery
} from 'uni-shared/query'

fxy060608's avatar
fxy060608 已提交
7 8 9
import {
  initData,
  initHooks,
DCloud-WZF's avatar
DCloud-WZF 已提交
10
  initUnknownHooks,
fxy060608's avatar
fxy060608 已提交
11 12 13 14 15 16 17 18 19
  handleEvent,
  initBehaviors,
  initVueComponent,
  PAGE_EVENT_HOOKS
} from 'uni-wrapper/util'

import {
  handleRef,
  handleLink,
20
  handleWrap,
fxy060608's avatar
fxy060608 已提交
21
  initBehavior,
22
  triggerEvent,
fxy060608's avatar
fxy060608 已提交
23
  initChildVues,
fxy060608's avatar
fxy060608 已提交
24
  initSpecialMethods
fxy060608's avatar
fxy060608 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
} from './util'

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

hooks.push(...PAGE_EVENT_HOOKS)

export default function parsePage (vuePageOptions) {
fxy060608's avatar
fxy060608 已提交
40
  const [VueComponent, vueOptions] = initVueComponent(Vue, vuePageOptions)
fxy060608's avatar
fxy060608 已提交
41 42 43 44

  const pageOptions = {
    mixins: initBehaviors(vueOptions, initBehavior),
    data: initData(vueOptions, Vue.prototype),
45
    onLoad (query) {
fxy060608's avatar
fxy060608 已提交
46 47 48 49 50 51 52 53 54 55
      const properties = this.props

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

      // 初始化 vue 实例
      this.$vm = new VueComponent(options)
56 57

      initSpecialMethods(this)
fxy060608's avatar
fxy060608 已提交
58 59 60 61

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

fxy060608's avatar
fxy060608 已提交
62 63 64
      const copyQuery = Object.assign({}, query)
      delete copyQuery.__id__

65
      this.$page = {
fxy060608's avatar
fxy060608 已提交
66
        fullPath: '/' + this.route + stringifyQuery(copyQuery)
67 68 69 70 71
      }

      this.options = query
      this.$vm.$mp.query = query // 兼容 mpvue
      this.$vm.__call_hook('onLoad', query)
fxy060608's avatar
fxy060608 已提交
72 73
    },
    onReady () {
fxy060608's avatar
fxy060608 已提交
74
      initChildVues(this)
fxy060608's avatar
fxy060608 已提交
75 76 77 78 79 80 81
      this.$vm._isMounted = true
      this.$vm.__call_hook('mounted')
      this.$vm.__call_hook('onReady')
    },
    onUnload () {
      this.$vm.__call_hook('onUnload')
      this.$vm.$destroy()
82 83 84 85 86 87
    },
    events: {
      // 支付宝小程序有些页面事件只能放在events下
      onBack () {
        this.$vm.__call_hook('onBackPress')
      }
fxy060608's avatar
fxy060608 已提交
88 89 90
    },
    __r: handleRef,
    __e: handleEvent,
91
    __l: handleLink,
92
    __w: handleWrap,
93
    triggerEvent
fxy060608's avatar
fxy060608 已提交
94 95
  }

96 97
  Object.assign(pageOptions.events, vuePageOptions.events || {})

fxy060608's avatar
fxy060608 已提交
98
  initHooks(pageOptions, hooks, vuePageOptions)
99
  initUnknownHooks(pageOptions, vuePageOptions, ['onReady'])
100 101 102 103 104 105 106 107

  if (Array.isArray(vueOptions.wxsCallMethods)) {
    vueOptions.wxsCallMethods.forEach(callMethod => {
      pageOptions[callMethod] = function (args) {
        return this.$vm[callMethod](args)
      }
    })
  }
fxy060608's avatar
fxy060608 已提交
108

fxy060608's avatar
fxy060608 已提交
109
  return pageOptions
110
}