page-parser.js 1.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 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 39
import {
  isPage,
  instances,
  initRelation
} from './util'

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

export default function parsePage (vuePageOptions) {
  const pageOptions = parseBasePage(vuePageOptions, {
    isPage,
    initRelation
  })
  // 页面需要在 ready 中触发,其他组件是在 handleLink 中触发
  pageOptions.lifetimes.ready = function ready () {
    if (this.$vm && this.$vm.mpType === 'page') {
      this.$vm.__call_hook('created')
      this.$vm.__call_hook('beforeMount')
      this.$vm._isMounted = true
      this.$vm.__call_hook('mounted')
      this.$vm.__call_hook('onReady')
    } else {
      this.is && console.warn(this.is + ' is not ready')
    }
  }

  pageOptions.lifetimes.detached = function detached () {
    this.$vm && this.$vm.$destroy()
    // 清理
    const pageId = this.pageinstance.__pageId__
    Object.keys(instances).forEach(key => {
      if (key.indexOf(pageId + '_') === 0) {
        delete instances[key]
      }
    })
  }

  return pageOptions
}