今天来看下 Vue3 在 createApp 这个方法执行的时候,都执行了哪些步骤?
import { createApp, ref } from \\\'vue\\\'
createApp({
setup(){
const hello = ref(\\\'this is vue3\\\');
return { hello }
}
}).mount(\\\'#app\\\')
//...args就是 setup 里面编写的代码
const createApp = ((...args) => {
const app = ensureRenderer().createApp(...args)
})
function baseCreateRenderer(
options: RendererOptions,
createHydrationFns?: typeof createHydrationFunctions
): any {
//move方法
//patch方法
//render方法
//...
return {
render,
hydrate,
createApp: createAppAPI(render, hydrate)
}
}
export function createAppAPI<HostElement>(
render: RootRenderFunction,
hydrate?: RootHydrateFunction
){
//接收两个参数:根组件和根props
return function createApp(rootComponent, rootProps = null) {
const app: App = (context.app = {
_uid: uid++,
_component: rootComponent as ConcreteComponent,
_props: rootProps,
_container: null,
_context: context,
version,
//... use、mixin、directive、mount、unmount、provide
return app
})
}
}
app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
const container = normalizeContainer(containerOrSelector) //标准化容器
if (!container) return
const component = app._component
if (!isFunction(component) && !component.render && !component.template) {
component.template = container.innerHTML
}
// clear content before mounting
container.innerHTML = \\\'\\\'
const proxy = mount(container)
if (container instanceof Element) {
container.removeAttribute(\\\'v-cloak\\\')
container.setAttribute(\\\'data-v-app\\\', \\\'\\\')
}
return proxy
}
[v-cloak] {
display: none !important;
}
<div id=\\\"app\\\" v-cloak></div>
container.removeAttribute(\\\'v-cloak\\\')
我会按执行逻辑,一步步分析 Vue3 源码,并持续输出为文字或视频。
图片授权基于 www.pixabay.com 相关协议
原创文章,作者:小道研究,如若转载,请注明出处:https://www.sudun.com/ask/34474.html