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

fxy060608's avatar
fxy060608 已提交
5 6 7 8
import {
  stringifyQuery
} from 'uni-shared/query'

9 10
import {
  isPage,
Q
qiang 已提交
11 12
  initRelation,
  mocks
fxy060608's avatar
fxy060608 已提交
13 14
} from './util'

Q
qiang 已提交
15 16 17 18
import {
  initMocks
} from 'uni-wrapper/util'

19 20 21 22 23
import {
  fixSetDataStart,
  fixSetDataEnd
} from '../../../mp-weixin/runtime/wrapper/fix-set-data'

fxy060608's avatar
fxy060608 已提交
24 25
import parseBaseComponent from '../../../mp-weixin/runtime/wrapper/component-base-parser'

26 27
const newLifecycle = swan.canIUse('lifecycle-2-0')

fxy060608's avatar
fxy060608 已提交
28
export default function parseComponent (vueOptions) {
fxy060608's avatar
fxy060608 已提交
29 30 31
  const componentOptions = parseBaseComponent(vueOptions, {
    isPage,
    initRelation
雪洛's avatar
雪洛 已提交
32 33 34 35
  })

  // 关于百度小程序生命周期的说明(组件作为页面时):
  // lifetimes:attached --> methods:onShow --> methods:onLoad --> methods:onReady
36
  // 这里在强制将onShow挪到onLoad之后触发,另外一处修改在page-parser.js
Q
qiang 已提交
37
  const oldAttached = componentOptions.lifetimes.attached
Q
qiang 已提交
38 39
  // 百度小程序基础库 3.260 以上支持页面 onInit 生命周期,提前创建 vm 实例
  componentOptions.lifetimes.onInit = function onInit (query) {
Q
qiang 已提交
40 41 42 43 44 45
    // 百度小程序后续可能移除 pageinstance 属性,为向后兼容进行补充
    if (!this.pageinstance || !this.pageinstance.setData) {
      const pages = getCurrentPages()
      this.pageinstance = pages[pages.length - 1]
    }

46
    // 处理百度小程序 onInit 生命周期调用 setData 无效的问题
47
    fixSetDataStart(this)
Q
qiang 已提交
48 49 50 51
    oldAttached.call(this)
    this.pageinstance.$vm = this.$vm
    this.$vm.__call_hook('onInit', query)
  }
fxy060608's avatar
fxy060608 已提交
52
  componentOptions.lifetimes.attached = function attached () {
Q
qiang 已提交
53 54
    if (!this.$vm) {
      oldAttached.call(this)
55
    } else {
Q
qiang 已提交
56
      initMocks(this.$vm, mocks)
57
      fixSetDataEnd(this)
Q
qiang 已提交
58
    }
Q
qiang 已提交
59
    if (isPage.call(this)) { // 百度 onLoad 在 attached 之前触发(基础库小于 3.70)
fxy060608's avatar
fxy060608 已提交
60 61
      // 百度 当组件作为页面时 pageinstancce 不是原来组件的 instance
      this.pageinstance.$vm = this.$vm
62
      if (hasOwn(this.pageinstance, '_$args')) {
fxy060608's avatar
fxy060608 已提交
63 64 65 66 67 68 69 70
        const query = this.pageinstance._$args
        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)
71
        this.$vm.__call_hook('onShow')
72
        delete this.pageinstance._$args
fxy060608's avatar
fxy060608 已提交
73
      }
雪洛's avatar
雪洛 已提交
74 75
    } else {
      // 百度小程序组件不触发methods内的onReady
76 77 78
      if (this.$vm) {
        this.$vm._isMounted = true
        this.$vm.__call_hook('mounted')
79
      }
fxy060608's avatar
fxy060608 已提交
80 81 82
    }
  }

雪洛's avatar
雪洛 已提交
83 84 85 86 87
  if (newLifecycle) {
    componentOptions.methods.onReady = componentOptions.lifetimes.ready
    delete componentOptions.lifetimes.ready
  }

fxy060608's avatar
fxy060608 已提交
88
  componentOptions.messages = {
fxy060608's avatar
fxy060608 已提交
89
    __l: componentOptions.methods.__l
fxy060608's avatar
fxy060608 已提交
90
  }
fxy060608's avatar
fxy060608 已提交
91
  delete componentOptions.methods.__l
fxy060608's avatar
fxy060608 已提交
92 93

  return componentOptions
Q
qiang 已提交
94
}