又看完一遍中文社區的教程接下來開始做vue2.0的源碼解讀了!
注:解讀源碼時一定要配合vue2.0的生命周期和API文檔一起看
vue2.0的生命周期分為4主要個過程
-
create。 創建---實例化Vue(new Vue) 時,會先進行create。
-
mount。掛載---根據el, template, render方法等屬性,會生成DOM,並添加到對應位置。
-
update。更新---當數據發生變化后,更新DOM。
-
destory。銷毀---銷毀時執行。
接下來再看看生命周期圖是不是很明朗呢?

源碼地址 https://github.com/vuejs/vue
帶你們進入源碼中看看new vue的簡略過程
第一步 new Vue({})
G:\vue-dev\src\core\instance\index.js
import { initMixin } from './init'
import { stateMixin } from './state'
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) //調用 init.js中 Vue.prototype._init
}
initMixin(Vue) //ctrl+鼠標左鍵跳入
stateMixin(Vue)
eventsMixin(Vue)
lifecycleMixin(Vue)
renderMixin(Vue)
export default Vue
注:以下源碼太長不做全部復制簡要截圖(注意我會截行數)源碼上去!
G:\vue-dev\src\core\instance\init.js

看到生命周期函數了吧!
在beforeCreate之前對 生命周期/事件/render 進行了初始化
beforeCreate和creted之間 執行的initState(vm) 函數 主要是對data/props/computed/watch等進行監聽

create完畢之后看mount
G:\vue-dev\src\core\instance\init.js

點擊$mount
進入G:\vue-dev\flow\component.js

可以看出執行是Component方法
在這個文件中你可以看到vue的幾乎全部接口,跳進來看了一遍發現應該是跳錯了 不過我還是熟悉了一下里面的額接口
然后我搜索 Vue.prototype.$mount 找到G:\vue-dev\src\entries\web-runtime-with-compiler.js\

我之前有說過上面的東西沒用么 沒有吧(死鴨子嘴硬着呢!)
G:\vue-dev\src\entries\web-runtime-with-compiler.js在改文件中主要對el, template, render 三個屬性進行處理
const options = this.$options // resolve template/el and convert to render function if (!options.render) { let template = options.template if (template) { if (typeof template === 'string') { if (template.charAt(0) === '#') { template = idToTemplate(template) /* istanbul ignore if */ if (process.env.NODE_ENV !== 'production' && !template) { warn( `Template element not found or is empty: ${options.template}`, this ) } } } else if (template.nodeType) { template = template.innerHTML } else { if (process.env.NODE_ENV !== 'production') { warn('invalid template option:' + template, this) } return this } } else if (el) { template = getOuterHTML(el) } if (template) { /* istanbul ignore if */ if (process.env.NODE_ENV !== 'production' && config.performance && mark) { mark('compile') } const { render, staticRenderFns } = compileToFunctions(template, { shouldDecodeNewlines, delimiters: options.delimiters }, this) options.render = render options.staticRenderFns = staticRenderFns /* istanbul ignore if */ if (process.env.NODE_ENV !== 'production' && config.performance && mark) { mark('compile end') measure(`${this._name} compile`, 'compile', 'compile end') } } } return mount.call(this, el, hydrating)
這里分享一個看源碼的小技巧
就是實在看不懂的時候去api 中找找也許能很好的幫助你理解 ----比如最后一個參數 (第一反應什么玩意啊)
有的時候甚至都不需要再回去看源碼了 哈哈

今天就看到這 有時間繼續
眼睛好疼啊!!!---可能發燒了
有人問 閱讀源碼有個卵用
如果你只是想用一下 當然沒啥卵用了,而且只要按路上照規范來工作中也不會遇見很深的問題 但是如果你想再前端方向上走得遠,玩的6666666你就需要深度閱讀一下了,
比如上個文件 可以看出 render>template>el的 哈哈 但是我今天看到說單文件組件(.vue)寫法 都會被整合成render的寫法
