component-parser.js 1.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
2 3 4 5 6
  hasOwn
} from 'uni-shared'

import {
  isPage,
fxy060608's avatar
fxy060608 已提交
7 8 9 10 11
  initRelation
} from './util'

import parseBaseComponent from '../../../mp-weixin/runtime/wrapper/component-base-parser'

12 13
const newLifecycle = swan.canIUse('lifecycle-2-0')

fxy060608's avatar
fxy060608 已提交
14
export default function parseComponent (vueOptions) {
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20 21
  const componentOptions = parseBaseComponent(vueOptions, {
    isPage,
    initRelation
  })

  const oldAttached = componentOptions.lifetimes.attached

fxy060608's avatar
fxy060608 已提交
22
  componentOptions.lifetimes.attached = function attached () {
fxy060608's avatar
fxy060608 已提交
23 24 25 26 27
    oldAttached.call(this)
    if (isPage.call(this)) { // 百度 onLoad 在 attached 之前触发
      // 百度 当组件作为页面时 pageinstancce 不是原来组件的 instance
      this.pageinstance.$vm = this.$vm

28
      if (hasOwn(this.pageinstance, '_$args')) {
29
        this.$vm.$mp.query = this.pageinstance._$args
30
        this.$vm.__call_hook('onLoad', this.pageinstance._$args)
31
        delete this.pageinstance._$args
fxy060608's avatar
fxy060608 已提交
32
      }
33 34
      // TODO  3.105.17以下基础库内百度 Component 作为页面时,methods 中的 onShow 不触发
      !newLifecycle && this.$vm.__call_hook('onShow')
35 36 37 38 39 40
    } else {
      // 百度小程序组件不触发methods内的onReady
      if (this.$vm) {
        this.$vm._isMounted = true
        this.$vm.__call_hook('mounted')
      }
41 42 43 44 45
    }
  }

  if (newLifecycle) {
    delete componentOptions.lifetimes.ready
fxy060608's avatar
fxy060608 已提交
46
    componentOptions.methods.onReady = function () {
47 48 49 50 51 52
      if (this.$vm) {
        this.$vm._isMounted = true
        this.$vm.__call_hook('mounted')
        this.$vm.__call_hook('onReady')
      } else {
        // this.is && console.warn(this.is + ' is not attached')
53
      }
fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59 60 61 62 63
    }
  }

  componentOptions.messages = {
    '__l': componentOptions.methods['__l']
  }
  delete componentOptions.methods['__l']

  return componentOptions
}