一. 从github获取vue开发版本的源码
git clone https://github.com/vuejs/vue.git
二. 从实例化vue (new vue)开始讲解
想了解“为什么Vue实例化后,通过this.能获取到data内的数据”必须对call函数内置方法有所了解,如有认识模糊的话可以参考JS的call方法的作用解释,简单易懂
1. new 实例化Vue首先进入的src/core/instance/index.js
// vue其实就是 function 实现的 class
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')
}
// new Vue进行初始化 Vue 并将options 参数传入_init 内 ,this._init 是原型中的方法 ,这个init方法来自于 ‘./init’ 中
this._init(options)
}
在实例化时,执行this._init(options),这个_init内置方法是下面initMixin(Vue)增加的
2. 进入src/core/instance/init.js 查看initMixin方法
initState(vm) // 初始化状态(这个状态包含data props methods), 这个初始化解决了实例化是通过 this 获取 data
<