create-page.js 1.6 KB
Newer Older
1 2
import Vue from 'vue'

3 4 5 6
import {
  handleLink
} from 'uni-platform/runtime/wrapper/index'

7 8 9
import {
  getData,
  initHooks,
fxy060608's avatar
fxy060608 已提交
10 11
  handleEvent,
  baiduPageDestroy
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
} from './util'

const hooks = [
  'onShow',
  'onHide',
  'onPullDownRefresh',
  'onReachBottom',
  'onShareAppMessage',
  'onPageScroll',
  'onResize',
  'onTabItemTap',
  'onBackPress',
  'onNavigationBarButtonTap',
  'onNavigationBarSearchInputChanged',
  'onNavigationBarSearchInputConfirmed',
  'onNavigationBarSearchInputClicked'
]

export function createPage (vueOptions) {
  vueOptions = vueOptions.default || vueOptions
  const pageOptions = {
fxy060608's avatar
fxy060608 已提交
33
    data: getData(vueOptions, Vue.prototype),
34
    onLoad (args) {
fxy060608's avatar
fxy060608 已提交
35 36
      if (__PLATFORM__ === 'mp-baidu') {
        this.$baiduComponentInstances = Object.create(null)
37 38
      }

fxy060608's avatar
fxy060608 已提交
39 40 41
      this.$vm = new Vue(Object.assign(vueOptions, {
        mpType: 'page',
        mpInstance: this
fxy060608's avatar
fxy060608 已提交
42
      }))
43

fxy060608's avatar
fxy060608 已提交
44
      this.$vm.__call_hook('created')
fxy060608's avatar
fxy060608 已提交
45
      this.$vm.__call_hook('onLoad', args) // 开发者可能会在 onLoad 时赋值,提前到 mount 之前
fxy060608's avatar
fxy060608 已提交
46
      this.$vm.$mount()
47 48
    },
    onReady () {
49
      this.$vm._isMounted = true
50
      this.$vm.__call_hook('mounted')
51 52 53 54
      this.$vm.__call_hook('onReady')
    },
    onUnload () {
      this.$vm.__call_hook('onUnload')
fxy060608's avatar
fxy060608 已提交
55 56 57 58 59
      if (__PLATFORM__ === 'mp-baidu') { // 百度组件不会在页面 unload 时触发 detached
        baiduPageDestroy(this.$vm)
      } else {
        this.$vm.$destroy()
      }
60 61 62 63 64 65 66 67 68
    },
    __e: handleEvent,
    __l: handleLink
  }

  initHooks(pageOptions, hooks)

  return Page(pageOptions)
}