vue解析系列(二):core vue & instance vue

293 阅读1分钟

core vue github.com/vuejs/vue/b…

import Vue from './instance/index'
// 引入初始化全局api函数
import {
  initGlobalAPI
} from './global-api/index'
// 引入服务端端判断变量
import {
  isServerRendering
} from 'core/util/env'
// 引入函数组件context构造函数
import {
  FunctionalRenderContext
} from 'core/vdom/create-functional-component'

// 添加静态方法
initGlobalAPI(Vue)

// 添加$isServer属性get方法
Object.defineProperty(Vue.prototype, '$isServer', {
  get: isServerRendering
})

// 添加$ssrContext属性get方法
Object.defineProperty(Vue.prototype, '$ssrContext', {
  get () {
    /* istanbul ignore next */
    return this.$vnode && this.$vnode.ssrContext
  }
})

// 添加FunctionalRenderContext属性的value
Object.defineProperty(Vue, 'FunctionalRenderContext', {
  value: FunctionalRenderContext
})

// rollup打包时替换为vue的版本号
Vue.version = '__VERSION__'

export default Vue

instance vue github.com/vuejs/vue/b…

import {
  initMixin
} from './init'
import {
  stateMixin
} from './state'
import {
  renderMixin
} from './render'
import {
  eventsMixin
} from './events'
import {
  lifecycleMixin
} from './lifecycle'
import {
  warn
} from '../util/index'

function Vue (options) { 
  if (process.env.NODE_ENV !== 'production' &&
    !(this instanceof Vue)
  ) {
    warn('Vue is a constructor and should be called with the `new` keyword')
  }
  this._init(options)
}

// 添加init方法
initMixin(Vue)

// 添加$props $data $watch $set $del属性
stateMixin(Vue)

// $on $once $emit $off
eventsMixin(Vue)

// _update $forceUpdate $destroy
lifecycleMixin(Vue)

// $nextTick _render
renderMixin(Vue)

export default Vue