当前位置:网站首页>render函数与虚拟dom
render函数与虚拟dom
2022-07-04 19:48:00 【bug丸】
render函数与虚拟dom
虚拟dom
是什么:用来描述dom的js对象
作用:vue在更新真实DOM前,虚拟dom会进行diff运算,比较更新前后虚拟DOM结构中的有差异的部分,然后采用异步更新队列的方式将差异的部分更新到真实DOM中,从而减少了最终要在真实DOM上执行的操作次数,提高了页面渲染的效率
let vDom = {
tag: "div",
attributes: {
id: "vdom",
},
children: {},
};
从模板到真实dom:
模板 -> 进行编译 -> 成render函数 -> 虚拟dom -> 真实dom
如果直接使用render函数,就省略了模板的编译过程,vue运行的更快
render函数
render函数就是用于实现虚拟dom的
使用render函数代替模板功能
不再需要使用vue内置指令,也没法使用,我们都可以通过原生js来实现
render函数通过createElement参数来创建虚拟dom;
createElement构成了vue virtual dom的模板,它有三个参数:
第一个参数必选:可以是一个html标签,也可以是一个组件或函数
第二个参数可选:数据对象,该dom节点的配置
第三个参数可选:组节点
<script>
export default {
render: (createElement) => {
return createElement(
// 第一个参数:必选,标签名
"div",
// 第二个参数:可选对象,标签相关信息
{
// 与 `v-bind:class` 的 API 相同,
// 接受一个字符串、对象或字符串和对象组成的数组
class: {
baseClass: true,
bar: false,
},
// 与 `v-bind:style` 的 API 相同,
// 接受一个字符串、对象,或对象组成的数组
style: {
color: "red",
fontSize: "16px",
},
// 普通的 HTML 特性
attrs: {
id: "foo",
},
// 组件 prop
props: {
myProp: "bar",
},
// DOM 属性
// domProps: {
// innerHTML: `爱你`,
// },
// 事件监听器在 `on` 属性内,
// 但不再支持如 `v-on:keyup.enter` 这样的修饰器。
on: {
click: function() {
console.log(6688);
},
},
},
// 第三个参数:可选数组,子节点相关
["爱你!", createElement("p", ["么么哒"])]
);
},
};
</script>
<style lang="less" scoped>
.baseClass {
background: #ccc;
}
</style>
.baseClass {
background: #ccc;
}
</style>
边栏推荐
- Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
- idea恢复默认快捷键
- idea插件
- Common verification rules of form components -1 (continuously updating ~)
- PermissionError: [Errno 13] Permission denied: ‘data.csv‘
- [in-depth learning] review pytoch's 19 loss functions
- Some suggestions for interface design
- From automation to digital twins, what can Tupo do?
- Practice examples to understand JS strong cache negotiation cache
- [1200. Différence absolue minimale]
猜你喜欢
uniapp 富文本编辑器使用
字节测试工程师十年经验直击UI 自动化测试痛点
Ten years' experience of byte test engineer directly hits the pain point of UI automation test
Automatic generation of interface automatic test cases by actual operation
The concept and application of hash table
Leetcode+ 81 - 85 monotone stack topic
MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
Understand the reading, writing and creation of files in go language
LeetCode+ 81 - 85 单调栈专题
Jiuqi ny8b062d MCU specification /datasheet
随机推荐
Idea case shortcut
卷积神经网络在深度学习中新发展的5篇论文推荐
WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
太方便了,钉钉上就可完成代码发布审批啦!
idea大小写快捷键
word中插入圖片後,圖片上方有一空行,且删除後布局變亂
Practice examples to understand JS strong cache negotiation cache
Win11U盘拒绝访问怎么办?Win11U盘拒绝访问的有效解决方法
Win11亮度被锁定怎么办?Win11亮度被锁定的解决方法
Function analysis and source code of hash guessing game system development
jekins初始化密码没有或找不到
In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
《动手学深度学习》(三) -- 卷积神经网络 CNN
idea恢复默认快捷键
左右最值最大差问题
How to solve the problem that win11 cannot write the value to the registry key?
Sword finger offer II 80-100 (continuous update)
[1200. Différence absolue minimale]
Understand the reading, writing and creation of files in go language
acwing 3302. Expression evaluation