1 Star 1 Fork 0

肖艳斌/vue_code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
12_3实例与生命周期.html 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
yanbin xiao 提交于 2024-03-21 20:44 . 修改文案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<div style="font-size: 14px; position:absolute;bottom:20px; left:10px;">
源代码地址:https://gitee.com/xiao_yan_bin/vue_code<br/>
github: https://github.com/xiaoyanbin/vue_code<br/>
vue设计与实现,持续更新,建议点赞收藏,有问题可以在评论区留言
</div>
</body>
<script>
function mountComponent(vnode, container,anchor) {
const componentOptions = vnode.type
const {render , data,beforeCreate, created, beforeMount, mounted,beforeUpdate,updated, } = componentOptions
beforeCreate && beforeCreate()
const state = reactive(data())
// 定义组件实例,
const instance = {
state,
isMounted: false,
subTree: null,
}
created && created.call(state)
// 将组件实例挂载到vnode上
vnode.component = instance
// 将组件的render函数调用包装到effect 内
effect(()=>{
const subTree = render.call(state, state)
if(!instance.isMounted){
beforeMount && beforeMount.call(state)
// 第一次挂载 传入null
patch(null,subTree, container, anchor)
// 更新是不会到这里
instance.isMounted = true
mounted && mounted.call(state)
}else{
// 挂载
beforeUpdate && beforeUpdate.call(state)
// instance.subTree 是上一次的subTree subTree是新的subTree container是容器 anchor是锚点
patch(instance.subTree,subTree, container, anchor)
updated && updated.call(state)
}
// 更新instance.subTree
instance.subTree = subTree
},{scheduler:queueJob})
}
</script>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/xiao_yan_bin/vue_code.git
git@gitee.com:xiao_yan_bin/vue_code.git
xiao_yan_bin
vue_code
vue_code
master

搜索帮助