page-parser.js 1.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
fxy060608's avatar
fxy060608 已提交
2
  instances,
3
  components
fxy060608's avatar
fxy060608 已提交
4 5 6 7 8
} from './util'

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

export default function parsePage (vuePageOptions) {
9
  const pageOptions = parseBasePage(vuePageOptions)
10 11 12 13 14 15 16 17 18
  const lifetimes = pageOptions.lifetimes
  const oldCreated = lifetimes.created
  lifetimes.created = function created () {
    const webviewId = this.__webviewId__
    components[webviewId] = []
    if (typeof oldCreated === 'function') {
      oldCreated.call(this)
    }
  }
fxy060608's avatar
fxy060608 已提交
19
  // 页面需要在 ready 中触发,其他组件是在 handleLink 中触发
20
  lifetimes.ready = function ready () {
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27 28 29
    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')
    }
30 31 32 33 34 35 36 37 38 39 40 41 42 43
  }
  const oldDetached = lifetimes.detached
  lifetimes.detached = function detached () {
    if (typeof oldDetached === 'function') {
      oldDetached.call(this)
    }
    // 清理
    const webviewId = this.__webviewId__
    webviewId && Object.keys(instances).forEach(key => {
      if (key.indexOf(webviewId + '_') === 0) {
        delete instances[key]
      }
    })
    delete components[webviewId]
fxy060608's avatar
fxy060608 已提交
44 45 46
  }

  return pageOptions
47
}