Vue源码浅析01

Vue源码大致可以理解分为:初始化属性、方法(Vue.use、***等等)和实例化(new Vue)执行的一些方法两大部分。

此系列文章主要是学习记录的笔记和大家分享,如果有不对的地方请及时指出。当然也存在很多没有搞明白的地方会先选择跳过后续补上。

大家首先可以去github上下载最新的vue源码,github地址:https://github.com/vuejs/vue

我们可以通过运行cnpm run dev来生成测试包。这样子大家在调试的时候如果有不懂的地方也方便打印log来调试查看。打包好的文件会保存在dist/vue.js。

本文还是主要介绍源码,这些准备工作就不再细化了。

这里整理的一份Vue的构造函数和原型上挂载的一些方法和属性:

//@instance/index.js
function Vue(){};
//instance/init.js
Vue.prototype._init = function(){}
//instance/state.js
const dataDef = {}
dataDef.get = function () { return this._data }
const propsDef = {}
propsDef.get = function () { return this._props }
Object.defineProperty(Vue.prototype, '$data', dataDef)
Object.defineProperty(Vue.prototype, '$props', propsDef)
Vue.prototype.$set = set
Vue.prototype.$delete = del
Vue.prototype.$watch = function (){}
//instance/events.js
Vue.prototype.$on = function (){}
Vue.prototype.$once = function (){}
Vue.prototype.$off = function (){}
Vue.prototype.$emit = function (){}
//instance/lifecycle.js
Vue.prototype._update = function (){}
Vue.prototype.$forceUpdate = function () {}
Vue.prototype.$destroy = function () {}
//instance/render.js
Vue.prototype.$nextTick = function () {}
Vue.prototype._render = function (){}

//@global-api/index.js
const configDef = {}
configDef.get = () => config
Object.defineProperty(Vue, 'config', configDef)
Vue.util = {
    warn,
    extend,
    mergeOptions,
    defineReactive
}
Vue.set = set
Vue.delete = del
Vue.nextTick = nextTick
Vue.options = Object.create(null)
['component', 'directive', 'filter'].forEach(type => {
    Vue.options[type + 's'] = Object.create(null)
})
Vue.options._base = Vue
extend(Vue.options.components, builtInComponents)
Object.defineProperty(Vue.prototype, '$isServer', {
  get: isServerRendering
})
Object.defineProperty(Vue.prototype, '$ssrContext', {
  get () {
    return this.$vnode && this.$vnode.ssrContext
  }
})
Object.defineProperty(Vue, 'FunctionalRenderContext', {
  value: FunctionalRenderContext
})
Vue.version = '__VERSION__'
//global-api/use.js
Vue.use = function(){}
//global-api/mixin.js
Vue.mixin = function(){}
//global-api/extend.js
Vue.extend = function(){}
//global-api/assets.js
['component', 'directive', 'filter'].forEach(type => {
    Vue[type] = function (){}
})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 由于第一周偷懒,这次总结了两周的情况。 财务方面 居家项目出现赤字。原因在于做预算时,没有将可能发生的支出考虑周全...
    苏果Susie阅读 1,468评论 1 1
  • 暑假已经过了半个月,刷作业刷书刷剧的生活天天过也过厌了。于是我和好友决定探索广州各个风格各异的书店(hh玩心大发)...
    huit阅读 2,350评论 0 1
  • 有亲密的朋友告诉我,你的蒙娜丽莎笑让人瘆得慌。 我的笑,给我精心准备的演出拉出一根臭烘烘的粑粑。 我是一个壳子,从...
    兰岚Lillian阅读 2,087评论 0 0
  • 有太多回忆的地方,不能停留太久。女孩总是活在回忆之中,女孩总是无法摆脱过去,人总是,也应该向前看,可女孩似乎总是会...
    大水牛泡泡阅读 2,844评论 0 0

友情链接更多精彩内容