引入Vue做了什么

执行如下代码:

import Vue from 'vue'
// 打印出引入的Vue
console.dir(Vue)

结果如下图:

点开prototype,结果如下图:

  1. 先执行了下面的5个mixin函数

  2. initMixin(Vue) // Vue.prototye上添加了_init方法

  3. stateMixin(Vue) // Vue.prototye上定义了属性: $data、$props,方法:$set、$delete、$watch

  4. eventsMixin(Vue)// Vue.prototye上添加了四个方法: $on、 $once 、$off 、$emit

  5. lifecycleMixin(Vue)// lifecycleMixin(Vue),在原型Vue.prototye上添加了三个方法:_update 、$forceUpdate 、$destory

  6. renderMixin(Vue)// Vue.prototye上添加了方法:$nextTick 、_render、 _o、 _n、 _s、 _l、 _t、 _q、 _i、 _m、 _f、 _k、 _b、 _v、 _e、 _u、 _g、 _d、 _p

  7. 然后执行如下代码,在src/core/index.js

initGlobalAPI(Vue):

思考: 为什么我们能够使用全局的api ?就是因为initGlobalAPI(Vue)这个函数,给Vue构造函数添加了这些全局的静态属性和方法

从上面的过程我们知道了,在项目中import Vue from vue时,除了给Vue的prototype添加不同的属性和方法,也给Vue添加了不同的属性和方法。下面我们在看看我们在new Vue()的时候做了什么下一章

Last updated

Was this helpful?