parseComponentOptions.ts 2.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import { hasOwn } from '@vue/shared'

D
DCloud_LXH 已提交
3 4 5 6 7
import {
  MPComponentInstance,
  MPComponentOptions,
  initMocks,
} from '@dcloudio/uni-mp-core'
fxy060608's avatar
fxy060608 已提交
8
import { ON_LOAD, ON_SHOW } from '@dcloudio/uni-shared'
D
DCloud_LXH 已提交
9 10 11 12
import {
  fixSetDataStart,
  fixSetDataEnd,
} from '../../../uni-mp-weixin/src/runtime/fixSetData'
fxy060608's avatar
fxy060608 已提交
13 14 15 16 17 18

export { handleLink, initLifetimes } from '@dcloudio/uni-mp-weixin'

export const mocks = ['nodeId', 'componentName', '_componentId', 'uniquePrefix']

export function isPage(mpInstance: MPComponentInstance) {
fxy060608's avatar
fxy060608 已提交
19
  return !hasOwn(mpInstance, 'ownerId')
fxy060608's avatar
fxy060608 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
}

export function initRelation(mpInstance: MPComponentInstance, detail: object) {
  ;(mpInstance as any).dispatch('__l', detail)
}

const newLifecycle = /*#__PURE__*/ swan.canIUse('lifecycle-2-0')

export function parse(componentOptions: MPComponentOptions) {
  const methods = componentOptions.methods as Record<
    string,
    (...args: any[]) => any
  >
  const lifetimes = componentOptions.lifetimes as Record<string, any>

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

    // 处理百度小程序 onInit 生命周期调用 setData 无效的问题
    fixSetDataStart(this as MPComponentInstance)
fxy060608's avatar
fxy060608 已提交
49
    oldAttached.call(this)
D
DCloud_LXH 已提交
50
    this.pageinstance.$vm = this.$vm
fxy060608's avatar
fxy060608 已提交
51
    this.$vm.$callHook('onInit', query)
D
DCloud_LXH 已提交
52 53 54 55 56 57 58 59
  }
  lifetimes.attached = function attached(this: MPComponentInstance) {
    if (!this.$vm) {
      oldAttached.call(this)
    } else {
      initMocks(this.$vm.$, this, mocks)
      fixSetDataEnd(this)
    }
fxy060608's avatar
fxy060608 已提交
60
    if (isPage(this) && this.$vm) {
D
DCloud_LXH 已提交
61
      // 百度 onLoad 在 attached 之前触发(基础库小于 3.70)
fxy060608's avatar
fxy060608 已提交
62 63 64 65
      // 百度 当组件作为页面时 pageinstance 不是原来组件的 instance
      const pageInstance = (this as any).pageinstance
      pageInstance.$vm = this.$vm
      if (hasOwn(pageInstance, '_$args')) {
fxy060608's avatar
fxy060608 已提交
66 67
        this.$vm.$callHook(ON_LOAD, pageInstance._$args)
        this.$vm.$callHook(ON_SHOW)
fxy060608's avatar
fxy060608 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        delete pageInstance._$args
      }
    } else {
      // 百度小程序组件不触发methods内的onReady
      if (this.$vm) {
        this.$vm.$callHook('mounted')
      }
    }
  }

  if (newLifecycle) {
    methods.onReady = lifetimes.ready
    delete lifetimes.ready
  }
  ;(componentOptions as any).messages = {
83
    __l: methods.__l,
fxy060608's avatar
fxy060608 已提交
84 85 86
  }
  delete methods.__l
}