index.js 1.6 KB
Newer Older
fxy060608's avatar
init v3  
fxy060608 已提交
1 2
import initVue from 'uni-core/vue'

fxy060608's avatar
fxy060608 已提交
3 4 5 6
import {
  initPolyfill
} from 'uni-core/service/plugins/polyfill'

fxy060608's avatar
init v3  
fxy060608 已提交
7 8 9 10 11 12 13 14 15 16 17 18
import {
  registerApp
} from '../app'

import {
  initData
} from './data'

import {
  initLifecycle
} from './lifecycle'

fxy060608's avatar
fxy060608 已提交
19 20 21 22
import {
  vdSyncCallbacks
} from '../subscribe-handlers/on-vd-sync-callback'

fxy060608's avatar
init v3  
fxy060608 已提交
23 24 25 26 27 28 29
export default {
  install (Vue, options) {
    initVue(Vue)

    initData(Vue)
    initLifecycle(Vue)

fxy060608's avatar
fxy060608 已提交
30 31
    initPolyfill(Vue)

32 33 34 35 36
    Object.defineProperty(Vue.prototype, '$page', {
      get () {
        return this.$root.$scope.$page
      }
    })
fxy060608's avatar
fxy060608 已提交
37 38 39 40
    // 兼容旧版本
    Object.defineProperty(Vue.prototype, '$mp', {
      get () {
        return {
fxy060608's avatar
fxy060608 已提交
41
          page: this.$root.$scope
fxy060608's avatar
fxy060608 已提交
42 43 44
        }
      }
    })
45

fxy060608's avatar
init v3  
fxy060608 已提交
46 47 48 49 50 51 52 53
    const oldMount = Vue.prototype.$mount
    Vue.prototype.$mount = function mount (el, hydrating) {
      if (this.mpType === 'app') {
        this.$options.render = function () {}
        registerApp(this)
      }
      return oldMount.call(this, el, hydrating)
    }
fxy060608's avatar
fxy060608 已提交
54 55 56

    Vue.prototype.$nextTick = function nextTick (cb) {
      const renderWatcher = this._watcher
57
      const callback = typeof cb === 'function'
fxy060608's avatar
fxy060608 已提交
58 59 60 61
      if (
        renderWatcher &&
        this._$queue.find(watcher => renderWatcher === watcher)
      ) {
62 63 64 65
        const result = new Promise((resolve) => {
          vdSyncCallbacks.push(callback ? cb.bind(this) : resolve)
        })
        return callback ? result : undefined
fxy060608's avatar
fxy060608 已提交
66
      } else {
fxy060608's avatar
fxy060608 已提交
67
        // $nextTick bind vm context
68
        return Vue.nextTick(callback ? () => cb.call(this) : undefined)
fxy060608's avatar
fxy060608 已提交
69 70
      }
    }
fxy060608's avatar
init v3  
fxy060608 已提交
71
  }
72
}