第十二章、实例方法与全局API的实现原理(1)

首先,我们来看一段代码:

import {initMixin} from './init'
import {startMixin} from './start'
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)
}

initMixin(Vue)
startMixin(Vue)
eventsMixin(Vue)
lifecycleMixin(Vue)
renderMixin(Vue)

export default Vue

上面这段代码中,这五个函数的作用就是向Vue的原型中挂载方法。

下面分别展示这五个函数的具体代码实现:

export function initMixin(Vue){
	Vue.prototype._init = function(options){
		// do something
	}
}
import {set, del} from '../observer/index'
export function startMixin(Vue){
	Vue.prototype.$set = set
	Vue.prototype.$delete = del
	Vue.prototype.$watch = function(expOrFn, cb, options) {}
}

export f

猜你喜欢

转载自blog.csdn.net/LiyangBai/article/details/104409124