いくつかのVueについての文章.(6)
3000 ワード
原文は私のblogをリンクして、STARを歓迎します.
前編ではmainで分析しました.jsは、
古いルール、私たちはまず文章を共有します.jsソース学習ノート.
この文章には
Vue、公式サイトのドキュメントを調べると、
本題に入ります.
で、Init、ファイルには、重要な手がかりがあります.
次にvm.$を見つけますmount()メソッド、
ソースの分析:
このソースコードから、Vue 2.0のテンプレートには
一周して、実はまた
終わります.
前編ではmainで分析しました.jsは、
el
属性によってマウントポイントを設定することもできるし、vm.$mount()
を手動で設定することもできる.この編では、底に深く入り込んで、原理を理解します.古いルール、私たちはまず文章を共有します.jsソース学習ノート.
この文章には
compile
と繰り返し言及されています.ええと.△何の鬼だ.Vue、公式サイトのドキュメントを調べると、
Vue
テンプレートがrender
関数にコンパイルされるプロセスをcompile
と言います.本題に入ります.
で、Init、ファイルには、重要な手がかりがあります.
_init
の最後に、initRender
メソッドが実行されます.このメソッドでは、el
属性が存在する場合、vm.$mount(vm.$options.el)
が実行され、未マウントのインスタンスがマウントされます.存在しない場合は、vm.$mount()
を介して未マウントのインスタンスが手動でマウントされている.次にvm.$を見つけますmount()メソッド、
ソースの分析:
// options.render , mount
//
if (!options.render) {
let template = options.template
// template , 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 , el
// template outHTML
template = getOuterHTML(el)
}
// template , compileToFunctions, render
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)
このソースコードから、Vue 2.0のテンプレートには
el, template, render
という3つの引用方法があります.優先度はrender > template > el
です.compileToFunctions()
を完了してテンプレートをrender
に変換すると、mountメソッドが開始されます.mount
の方法では、 vm._watcher = new Watcher(vm, updateComponent, noop)
updateComponent = () => {
vm._update(vm._render(), hydrating)
}
一周して、実はまた
render
関数からvnode
の部分に戻って、ここでは3編目で詳しく説明して、もう繰り返しません.終わります.