- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
Vue.createApp(App).mount('#app')执行过程
1.创建app入口
2.render函数
3.返回app
mount
App组件的挂载和渲染过程
patch:进行diff算法.
处理组件节点.
调用mountComponent挂载组件
初始化component,对组件的数据进行赋值和操作
调用设置和渲染的副作用函数
调用了副作用函数effect()
renderComponentRoot,对template进行渲染
把subTree子树的VNode挂载到container上,调用patch()
处理DOM元素,比如div/button/span(每个子树subtree)
如果还有子元素,进行patch之后,进行挂载
11.12.13. 处理子节点,进行挂载
来到runtime-dom>src>index.ts文件
// 创建app入口
export const createApp = ((...args) => {
// 创建app使用渲染器ensureRenderer()
// 渲染器有返回createApp属性,createApp调用之后返回app
const app = ensureRenderer().createApp(...args)
if (__DEV__) {
injectNativeTagCheck(app)
injectCompilerOptionsCheck(app)
}
// 从app取出mount
const { mount } = app
// 将app.mount函数进行重写,函数原本传入属性,而我们传入‘#app’字符串
// 重写mount函数是为了跨平台
app.mount = (containerOrSelector: Element | ShadowRoot | string): any => {
// normalizeContainer通过querySelector获取到container对象
const container = normalizeContainer(containerOrSelector)
if (!container) return
const component = app._component
if (!isFunction(component) && !component.render && !component.template) {
// __UNSAFE__
// Reason: potential execution of JS expressions in in-DOM template.
// The user must make sure the in-DOM template is trusted. If it's
// rendered by the server, the template should not contain any user data.
// 原因:在 DOM 模板中可能执行 JS 表达式。
// 用户必须确保 DOM 模板是受信任的。如果是
// 由服务器呈现,模板不应包含任何用户数据。
component.template = container.innerHTML
// 2.x compat check
if (__COMPAT__ && __DEV__) {
for (let i = 0; i < container.attributes.length; i++) {
const attr = container.attributes[i]
if (attr.name !== 'v-cloak' && /^(v-|:|@)/.test(attr.name)) {
compatUtils.warnDeprecation(
DeprecationTypes.GLOBAL_MOUNT_CONTAINER,
null
)
break
}
}
}
}
// clear content before mounting
// 先清空container中的原本内容
container.innerHTML = ''
const proxy = mount(container, false, container instanceof SVGElement)
if (container instanceof Element) {
container.removeAttribute('v-cloak')
container.setAttribute('data-v-app', '')
}
return proxy
}
return app
}) as CreateAppFunction<Element>
来到runtime-core>src>renderer.ts文件
// render函数传入baseCreatoRenderer()的返回值中
const render: RootRenderFunction = (vnode, container, isSVG) => {
if (vnode == null) {
if (container._vnode) {
unmount(container._vnode, null, null, true)
}
} else {
patch(container._vnode || null, vnode, container, null, null, null, isSVG)
}
flushPostFlushCbs()
container._vnode = vnode
}
const internals: RendererInternals = {
p: patch,
um: unmount,
m: move,
r: remove,
mt: mountComponent,
mc: mountChildren,
pc: patchChildren,
pbc: patchBlockChildren,
n: getNextHostNode,
o: options
}
let hydrate: ReturnType<typeof createHydrationFunctions>[0] | undefined
let hydrateNode: ReturnType<typeof createHydrationFunctions>[1] | undefined
if (createHydrationFns) {
;[hydrate, hydrateNode] = createHydrationFns(
internals as RendererInternals<Node, Element>
)
}
// baseCreatoRenderer()返回 createApp
return {
render,
hydrate,//ssr服务端错误
// createAppAPI函数使用了柯里化
createApp: createAppAPI(render, hydrate)
}
}
来到runtime-core>src>apiCreateApp.ts文件
// createApp最终返回createAppAPI() 返回app对象
export function createAppAPI<HostElement>(
render: RootRenderFunction,
hydrate?: RootHydrateFunction
): CreateAppFunction<HostElement> {
// 返回createApp
return function createApp(rootComponent, rootProps = null) {
if (!isFunction(rootComponent)) {
rootComponent = { ...rootComponent }
}
if (rootProps != null && !isObject(rootProps)) {
__DEV__ && warn(`root props passed to app.mount() must be an object.`)
rootProps = null
}
const context = createAppContext()
const installedPlugins = new Set()
let isMounted = false
// app对象 app.mixin.directive.comoinebt 创建一个app对象
const app: App = (context.app = {
_uid: uid++,
_component: rootComponent as ConcreteComponent,
_props: rootProps,
_container: null,
_context: context,
_instance: null,
version,
get config() {
return context.config
},
set config(v) {
if (__DEV__) {
warn(
`app.config cannot be replaced. Modify individual options instead.`
)
}
},
use(plugin: Plugin, ...options: any[]) {
if (installedPlugins.has(plugin)) {
__DEV__ && warn(`Plugin has already been applied to target app.`)
} else if (plugin && isFunction(plugin.install)) {
installedPlugins.add(plugin)
plugin.install(app, ...options)
} else if (isFunction(plugin)) {
installedPlugins.add(plugin)
plugin(app, ...options)
} else if (__DEV__) {
warn(
`A plugin must either be a function or an object with an "install" ` +
`function.`
)
}
return app
},
mixin(mixin: ComponentOptions) {
if (__FEATURE_OPTIONS_API__) {
if (!context.mixins.includes(mixin)) {
context.mixins.push(mixin)
} else if (__DEV__) {
warn(
'Mixin has already been applied to target app' +
(mixin.name ? `: ${mixin.name}` : '')
)
}
} else if (__DEV__) {
warn('Mixins are only available in builds supporting Options API')
}
return app
},
component(name: string, component?: Component): any {
if (__DEV__) {
validateComponentName(name, context.config)
}
if (!component) {
return context.components[name]
}
if (__DEV__ && context.components[name]) {
warn(`Component "${name}" has already been registered in target app.`)
}
context.components[name] = component
return app
},
directive(name: string, directive?: Directive) {
if (__DEV__) {
validateDirectiveName(name)
}
if (!directive) {
return context.directives[name] as any
}
if (__DEV__ && context.directives[name]) {
warn(`Directive "${name}" has already been registered in target app.`)
}
context.directives[name] = directive
return app
},
mount(
rootContainer: HostElement,
isHydrate?: boolean,
isSVG?: boolean
): any {
if (!isMounted) {
// #5571
if (__DEV__ && (rootContainer as any).__vue_app__) {
warn(
`There is already an app instance mounted on the host container.\n` +
` If you want to mount another app on the same host container,` +
` you need to unmount the previous app by calling \`app.unmount()\` first.`
)
}
const vnode = createVNode(
rootComponent as ConcreteComponent,
rootProps
)
// store app context on the root VNode.
// this will be set on the root instance on initial mount.
vnode.appContext = context
// HMR root reload
if (__DEV__) {
context.reload = () => {
render(cloneVNode(vnode), rootContainer, isSVG)
}
}
if (isHydrate && hydrate) {
hydrate(vnode as VNode<Node, Element>, rootContainer as any)
} else {
render(vnode, rootContainer, isSVG)
}
isMounted = true
app._container = rootContainer
// for devtools and telemetry
;(rootContainer as any).__vue_app__ = app
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
app._instance = vnode.component
devtoolsInitApp(app, version)
}
return getExposeProxy(vnode.component!) || vnode.component!.proxy
} else if (__DEV__) {
warn(
`App has already been mounted.\n` +
`If you want to remount the same app, move your app creation logic ` +
`into a factory function and create fresh app instances for each ` +
`mount - e.g. \`const createMyApp = () => createApp(App)\``
)
}
},
unmount() {
if (isMounted) {
render(null, app._container)
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
app._instance = null
devtoolsUnmountApp(app)
}
delete app._container.__vue_app__
} else if (__DEV__) {
warn(`Cannot unmount an app that is not mounted.`)
}
},
provide(key, value) {
if (__DEV__ && (key as string | symbol) in context.provides) {
warn(
`App already provides property with key "${String(key)}". ` +
`It will be overwritten with the new value.`
)
}
context.provides[key as string | symbol] = value
return app
}
})
if (__COMPAT__) {
installAppCompatProperties(app, context, render)
}
return app
}
}
来到runtime-core>src>renderer.ts文件
// 如果还有子元素,进行pathch之后,进行挂载
mountChildren(
vnode.children as VNodeArrayChildren,
el,
null,
parentComponent,
parentSuspense,
isSVG && type !== 'foreignObject',
slotScopeIds,
optimized
)
const mountChildren: MountChildrenFn = (
children,
container,
anchor,
parentComponent,
parentSuspense,
isSVG,
slotScopeIds,
optimized,
start = 0
) => {
// 12.对子节点进行遍历
for (let i = start; i < children.length; i++) {
const child = (children[i] = optimized
? cloneIfMounted(children[i] as VNode)
: normalizeVNode(children[i]))
// 13。遍历后进行挂载
patch(
null,
child,
container,
anchor,
parentComponent,
parentSuspense,
isSVG,
slotScopeIds,
optimized
)
}
}
//片段只能有数组子级 因为它们要么由编译器生成,要么隐式创建 从数组。
// 拿到fragment的所有根组件,拿到子树元素,进行挂载
mountChildren(
// n2.children所有的子元素
n2.children as VNodeArrayChildren,
container,
fragmentEndAnchor,
parentComponent,
parentSuspense,
isSVG,
slotScopeIds,
optimized
)
创建流程图:
大致分为13步,具体如下:
// 注意:此闭包中的函数应使用 'const xxx = () => {}'
// 样式,以防止被小写器内联。
// patch:进行diff算法,crateApp->vnode->element
const patch: PatchFn = (
n1,//旧的vnode,n1为空,进行挂载
n2,//新的vnode,n2与n1不同时,进行patch(diff算法)
container,//patch或mount之后,将vnode挂载到container上
anchor = null,
parentComponent = null,
parentSuspense = null,
isSVG = false,
slotScopeIds = null,
optimized = __DEV__ && isHmrUpdating ? false : !!n2.dynamicChildren
) => {
} else if (shapeFlag & ShapeFlags.COMPONENT) {
// 处理组件节点,vnode-><HelloWorld> 按组件进行处理
processComponent(
n1,
n2,
container,
anchor,
parentComponent,
parentSuspense,
isSVG,
slotScopeIds,
optimized
)
}
进入processComponent
// 处理组件类型节点
const processComponent = (
n1: VNode | null,
n2: VNode,
container: RendererElement,
anchor: RendererNode | null,
parentComponent: ComponentInternalInstance | null,
parentSuspense: SuspenseBoundary | null,
isSVG: boolean,
slotScopeIds: string[] | null,
optimized: boolean
) => {
// 调用mountComponent挂载组件
mountComponent(
n2,
container,
anchor,
parentComponent,
parentSuspense,
isSVG,
optimized
)
}
// 初始化component,对组件的数据进行赋值和操作
// setup组件实例,对组件的props/emits/slots/data进行初始化
setupComponent(instance)//执行完之后组件啥都有了
if (__DEV__) {
endMeasure(instance, `init`)
}
}
// setup() is async. This component relies on async logic to be resolved
// before proceeding
// setup() 是异步的。此组件依赖于要解析的异步逻辑
// 在继续之前
if (__FEATURE_SUSPENSE__ && instance.asyncDep) {
parentSuspense && parentSuspense.registerDep(instance, setupRenderEffect)
// Give it a placeholder if this is not hydration
// TODO handle self-defined fallback
if (!initialVNode.el) {
const placeholder = (instance.subTree = createVNode(Comment))
processCommentNode(null, placeholder, container!, anchor)
}
return
}
// 调用设置和渲染的副作用函数
setupRenderEffect(
instance,
initialVNode,
container,
anchor,
parentSuspense,
isSVG,
optimized
)
副作用函数
// 副作用函数
const setupRenderEffect: SetupRenderEffectFn = (
instance,
initialVNode,
container,
anchor,
parentSuspense,
isSVG,
optimized
) => {
// 调用了副作用函数effect()他来着reactive响应式系统,使用响应式数据时,默认进行依赖收集,进行再次执行
const update: SchedulerJob = (instance.update = () => effect.run())
update.id = instance.uid
// allowRecurse
// #1801, #2043 component render effects should allow recursive updates
//组件渲染效果应允许递归更新
toggleRecurse(instance, true)
if (__DEV__) {
effect.onTrack = instance.rtc
? e => invokeArrayFns(instance.rtc!, e)
: void 0
effect.onTrigger = instance.rtg
? e => invokeArrayFns(instance.rtg!, e)
: void 0
update.ownerInstance = instance
}
// 7. renderComponentRoot,对template进行渲染
// subTree是template里面的子树,子树被<Fragment>包裹
instance.subTree = renderComponentRoot(instance)
if (__DEV__) {
endMeasure(instance, `render`)
}
if (__DEV__) {
startMeasure(instance, `hydrate`)
}
// 8. 把subTree子树的VNode挂载到container上,调用patch()
patch(
null,//因为是挂载所以是null
subTree,
container,
anchor,
instance,
parentSuspense,
isSVG
)
// 9. 处理DOM元素,比如div/button/span(每个子树subtree)
if (shapeFlag & ShapeFlags.ELEMENT) {
processElement(
n1,
n2,
container,
anchor,
parentComponent,
parentSuspense,
isSVG,
slotScopeIds,
optimized
)
进入processElement
// 如果template的根元素是div
const processElement = (
n1: VNode | null,
n2: VNode,
container: RendererElement,
anchor: RendererNode | null,
parentComponent: ComponentInternalInstance | null,
parentSuspense: SuspenseBoundary | null,
isSVG: boolean,
slotScopeIds: string[] | null,
optimized: boolean
) => {
isSVG = isSVG || (n2.type as string) === 'svg'
if (n1 == null) {
// 调用mountElement进行挂载元素
mountElement(
n2,
container,
anchor,
parentComponent,
parentSuspense,
isSVG,
slotScopeIds,
optimized
)
} else {
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
// 如果还有子元素,进行pathch之后,进行挂载
mountChildren(
vnode.children as VNodeArrayChildren,
el,
null,
parentComponent,
parentSuspense,
isSVG && type !== 'foreignObject',
slotScopeIds,
optimized
)
}
// 11.挂载子节点
const mountChildren: MountChildrenFn = (
children,
container,
anchor,
parentComponent,
parentSuspense,
isSVG,
slotScopeIds,
optimized,
start = 0
) => {
// 12.对子节点进行遍历
for (let i = start; i < children.length; i++) {
const child = (children[i] = optimized
? cloneIfMounted(children[i] as VNode)
: normalizeVNode(children[i]))
// 13。遍历后进行挂载
patch(
null,
child,
container,
anchor,
parentComponent,
parentSuspense,
isSVG,
slotScopeIds,
optimized
)
}
}
最后给大家找了个流程图方便大家理解:
**如果大家觉得还不错,下方公z号👇,来跟作者一起学习吧! **
我正在尝试创建1-click快捷方式,以便在上小学时玩一些90年代的旧游戏。 我正在尝试将ISO挂载到特定驱动器 运行程序 确保已卸载驱动器,以清除下一个要清除的游戏 我是脚本新手,所以我不确定从哪里
我正在运行 docker compose 命令以在 docker 中运行应用程序。但是,yml 中定义的卷是空的。如果我挂载一个文件,它就可以工作。但是,当我从 Windows 挂载目录时,我可以看到
我有一个双向滚动的无限滚动列表。该列表显示每行 5 个项目的网格,表示一周 7 天中的 5 个工作日。日子是按月剥离的斑马线(甚至月份的颜色略深)。我想将月份标题放在网格左侧的一列中,从该月的第一天或
我在按照 qemu/linaro 教程尝试执行 qemu, https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Vir
我正在尝试挂载 cephfs,密码没问题: mount -t ceph ceph-mon:6789:/docker/mnt/cephfs -o name=admin,secret=admin-pass
我可以将 HDFS 目录(在 Ubuntu 中配置)挂载到 Windows 服务器的驱动器吗? 映射后,例如 H:\--->\home\user1\HDFSCreatedDir ,想使用普通的 Jav
我有一个由 500 个 linux 机器组成的集群,现在需要使用带有绑定(bind)选项的挂载资源(参见 man 8 mount)来支持 chroot jail 。安装点需要在引导后强制执行和维护。我
我在一些代码片段和 Requests documentation 中看到过类似的事情。 : import requests sess = requests.Session() adapter = re
嗨,我正在编写一个安装cgroup的应用程序,如下所示 mount("cgroup", "/sys/fs/cgroup", "tmpfs",0,NULL); 我可以执行此操作,但是我想添加一个检查以了
我希望使用 Powershell 获取 VHD 安装的驱动器号。我可以使用以下 cmdlet 挂载 VHD: Mount-VHD -Path d:/tmp.vhdx 安装工作正常,但是当我尝试获取驱动
我正在编写一个脚本来创建坐骑。我使用的系统命令是: sudo /bin/mount -soft -t smbfs -o username='{username}',password='{passwor
我正在尝试在多个用户之间共享 NFS 安装。我无法让它工作,因为我总是被拒绝访问。我可以挂载共享,但看不到文件。 导出是通过Heartbeat+Pacemaker进行的。我认为这没有什么区别,但这是导
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 此问题似乎与 a specific programming problem, a sof
我最近全新安装了elementary OS,与Windows 8.1双启动。安装完成后,我编辑了eOS的fstab文件,使其自动在/mnt/Windows地址挂载一个ntfs分区。后来我关闭了笔记本电
我正在尝试在装有 Android 2.1 的模拟器上分析 Android 恶意软件。我想在执行可疑应用程序后分析文件权限和指纹。我知道,我可以使用 adb shell 来获取此信息,但我认为在执行例如
在使用 Webpack 和 Vue 编译项目后,当我打开一个使用 Vue 组件的页面时,我得到: [Vue warn]: Failed to mount component: template or
我正在使用 Linux Inotify 来检测程序上的 FS 事件。 当设备挂载到监控目录时如何通知我? 最佳答案 我不认为你可以用 inotify 来做到这一点。这是方法: 阅读uevents fr
有几篇文章对理解 Docker 的卷和数据管理非常有帮助。这两个尤其出色: http://container-solutions.com/understanding-volumes-docker/ h
我正在使用 mount -o bind /some/directory/here /foo/bar 我想用 bash 脚本检查 /foo/bar,看看它是否已经挂载?如果不是,则调用上面的 mount
我是一名优秀的程序员,十分优秀!