page-parser.js 1.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
import {
  stringifyQuery
} from 'uni-shared/query'

fxy060608's avatar
fxy060608 已提交
5
import {
Q
qiang 已提交
6
  isPage,
fxy060608's avatar
fxy060608 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  initRelation
} from './util'

import parseBasePage from '../../../mp-weixin/runtime/wrapper/page-base-parser'

function detached ($vm) {
  $vm.$children.forEach(childVm => {
    childVm.$scope.detached()
  })
  $vm.$scope.detached()
}

function onPageUnload ($vm) {
  $vm.$destroy()
  $vm.$children.forEach(childVm => {
    detached(childVm)
  })
}

fxy060608's avatar
fxy060608 已提交
26
export default function parsePage (vuePageOptions) {
fxy060608's avatar
fxy060608 已提交
27 28 29
  const pageOptions = parseBasePage(vuePageOptions, {
    isPage,
    initRelation
Q
qiang 已提交
30 31
  })

32
  // 纠正百度小程序生命周期methods:onShow在methods:onLoad之前触发的问题
fxy060608's avatar
fxy060608 已提交
33 34 35 36
  pageOptions.methods.onShow = function onShow () {
    if (this.$vm && this.$vm.$mp.query) {
      this.$vm.__call_hook('onShow')
    }
37
  }
fxy060608's avatar
fxy060608 已提交
38

fxy060608's avatar
fxy060608 已提交
39
  pageOptions.methods.onLoad = function onLoad (query) {
Q
qiang 已提交
40
    // 百度 onLoad 在 attached 之前触发(基础库小于 3.70),先存储 args, 在 attached 里边触发 onLoad
Q
qiang 已提交
41
    if (this.$vm) {
fxy060608's avatar
fxy060608 已提交
42 43 44 45 46 47 48
      const copyQuery = Object.assign({}, query)
      delete copyQuery.__id__
      this.pageinstance.$page = this.$page = {
        fullPath: '/' + this.pageinstance.route + stringifyQuery(copyQuery)
      }
      this.$vm.$mp.query = query
      this.$vm.__call_hook('onLoad', query)
Q
qiang 已提交
49 50
      this.$vm.__call_hook('onShow')
    } else {
fxy060608's avatar
fxy060608 已提交
51
      this.pageinstance._$args = query
fxy060608's avatar
fxy060608 已提交
52
    }
fxy060608's avatar
fxy060608 已提交
53 54 55 56 57 58 59 60
  }

  pageOptions.methods.onUnload = function onUnload () {
    this.$vm.__call_hook('onUnload')
    onPageUnload(this.$vm)
  }

  return pageOptions
Q
qiang 已提交
61
}