Vue 入口
Vue的源码使用flow开发,使用rollup进行构建。这里使用开发版本,查看package.json
1 | "dev": "rollup -w -c scripts/config.js --environment TARGET:web-full-dev --sourcemap", |
target为web-full-dev,查看scripts/config.js中配置:
1 | 'web-full-dev': { |
开发模式版本使用的entry-runtime-with-compiler编译,可以查看到Vue入口
Vue的入口在 entry-runtime-with-compiler -> /runtime/index' -> /instance/index
Vue核心文件在instance/index中
1 | import { initMixin } from './init' |
initMixin 初始化
initMixin就做了一件事,就是给Vue对象赋值了一个_init内部方法。_init方法初始化内部组件,或者合并options属性
1 | export function initMixin (Vue: Class<Component>) { |
stateMixin
stateMixin干的事情也不多,就三个事情:
1 设置vm的$data和$props的代理方法
2 定义Vue的$watch原型方法
3 设置Vue的$set和$delete方法, 而set和del方法都在observer/index中定义,主要用于对特定的属性进行纳入响应式,比如数组元素,或者后加入的元素。
1 | export function stateMixin (Vue: Class<Component>) { |
eventsMixin
主要用于定义事件方法比如,$once,$on, $emit, $off, 其实就是实现了一个事件系统
1 | Vue.prototype.$emit = function (event: string): Component { |
lifecycleMixin
定义三个方法,_update, $desotry, $forceUpdate原型方法
_update方法调用patch方法进行patch
1 | Vue.prototype._update = function (vnode: VNode, hydrating?: boolean) { |
forceUpdate方法手动触发watcher的update方法
1 | Vue.prototype.$forceUpdate = function () { |
destroy卸载节点
1 | Vue.prototype.$destroy = function () { |
renderMixin
定义两个原型方法,$nextTick,以及内部_render方法
1 | installRenderHelpers(Vue.prototype) // 设置vue helper方法 |
这个_render方法会在后面渲染组件处被用到,其实主要还用调用了后面编译之后的render方法来进行调用,这里只是做了一些预先的处理,比如作用域插槽,错误捕捉,以及父节点的处理
1 | Vue.prototype._render = function (): VNode { |