当前位置: 首页 > news >正文

vue render 函数详解 (配参数详解)

vue render 函数详解 (配参数详解)



在 Vue 3 中,`render` 函数被用来代替 Vue 2 中的模板语法。 它接收一个 h 函数(或者是 `createElement` 函数的别名),并且返回一个虚拟 DOM。

render 函数的语法结构如下:

render(h) {return h('div', { class: 'container' }, 'Hello, World!')
}

在上面的示例中,我们使用 h 函数创建了一个 div 元素,并设置了 class 属性,并且在 div 元素中添加了文本内容 “Hello, World!”。

h 函数的使用方式如下:

h(tag, data, children)
  • tag:表示要创建的元素的标签名,可以是字符串或者是组件选项对象。
  • data:表示要添加到元素的属性、事件等。
  • children:表示要作为子节点添加到元素中的内容。

data 可以包含普通 HTML 属性、DOM 属性、事件、样式等。例如:

render(h) {return h('div', { class: 'container',style: { color: 'red' },on: {click: () => {console.log('Clicked!')}}}, 'Hello, World!')
}

在上面的示例中,我们设置了 class 属性为 'container',样式为红色,以及点击事件监听器。当用户点击该元素时,控制台会打印 “Clicked!”。

除了使用原生的 HTML 标签,我们还可以使用组件选项对象来创建组件。例如:

const MyComponent = {render(h) {return h('div', 'Hello, Component!')}
}render(h) {return h(MyComponent)
}

在上面的示例中,我们定义了一个名为 MyComponent 的组件,然后在 render 函数中使用 h 函数创建了该组件。这将会渲染出一个 div 元素,内容为 “Hello, Component!”。

需要注意的是,在 Vue 3 中,所有组件都需要使用 defineComponent 函数包裹起来,以便能够正确使用组件特定的 API。

import { defineComponent } from 'vue'const MyComponent = defineComponent({render(h) {return h('div', 'Hello, Component!')}
})
以下是h函数的第二个参数属性的详解(class、style、attrs、props、on、nativeOn、directives、slot、key、ref、scopedSlots):
  1. class - 设置元素的CSS类名,可以使用字符串或对象。对象的键是类名,值是一个布尔值,用于动态地添加或移除类名。
h('div', {class: 'red'
})
// 创建<div class="red"></div>h('div', {class: {red: true,bold: false}
})
// 创建<div class="red"></div>
  1. style - 设置元素的内联样式,可以使用字符串、对象或数组。
h('div', {style: 'color: red;'
})
// 创建<div style="color: red;"></div>h('div', {style: {color: 'red',fontSize: '14px'}
})
// 创建<div style="color: red; font-size: 14px;"></div>h('div', {style: [{ color: 'red' },{ fontSize: '14px' }]
})
// 创建<div style="color: red; font-size: 14px;"></div>
  1. attrs - 设置元素的属性,可以使用对象或数组。
h('input', {attrs: {type: 'text',placeholder: 'Enter text'}
})
// 创建<input type="text" placeholder="Enter text">h('div', {attrs: [{ id: 'my-id' },{ 'data-custom-attr': 'value' }]
})
// 创建<div id="my-id" data-custom-attr="value"></div>
  1. props - 设置元素的DOM属性,与attrs类似,但是props适用于组件的props。
h('my-component', {props: {message: 'Hello world'}
})
// 创建<my-component message="Hello world"></my-component>
  1. on - 绑定事件处理函数,可以使用对象或数组。
h('button', {on: {click: handleClick}
})
// 创建<button @click="handleClick"></button>h('div', {on: [{ click: handleClick },{ mouseover: handleMouseOver }]
})
// 创建<div @click="handleClick" @mouseover="handleMouseOver"></div>
  1. nativeOn - 属性用于指定元素的原生事件监听器,即直接绑定到DOM元素上的事件,而不是绑定到组件上的自定义事件。
import Vue from 'vue';Vue.component('my-component', {render(h) {return h('button', {nativeOn: {click: this.handleClick}}, 'Click me');},methods: {handleClick() {console.log('Button clicked');}}
});new Vue({el: '#app'
});
  1. domProps - 设置元素的DOM属性,比如innerHTML、textContent等。
h('span', {domProps: {textContent: 'Hello'}
})
// 创建<span>Hello</span>h('div', {domProps: {innerHTML: '<p>Paragraph</p>'}
})
// 创建<div><p>Paragraph</p></div>
  1. key - 用于VNode的唯一标识,用于在列表渲染中进行优化。
h('div', {key: 'my-key',class: 'red'
})
// 创建<div key="my-key" class="red"></div>
  1. ref - 用于给元素或组件设置一个引用标识,以便通过$refs属性访问。
h('input', {ref: 'myInput',attrs: {type: 'text'}
})
// 创建<input ref="myInput" type="text">h('my-component', {ref: 'myComponent'
})
// 创建<my-component ref="myComponent"></my-component>
  1. slot - 用于分发内容到组件的插槽。
h('my-component', [h('div', {slot: 'header'}, 'Header content'),h('div', {slot: 'footer'}, 'Footer content')
])
// 创建<my-component><div slot="header">Header content</div><div slot="footer">Footer content</div></my-component>
  1. scopedSlots - 属性是一个包含插槽信息的对象。它的每个键是插槽的名称,对应的值是一个函数或者一个具有render函数的对象。
// 示例组件
const MyComponent = {render(h) {return h('div', [h('h1', 'Hello World'),h('slot', {// 插槽名称为defaultscopedSlots: {default: props => h('p', `Scoped Slot Content: ${props.text}`)}})])}
}// 父组件
new Vue({render(h) {return h('my-component', {// 通过scopedSlots属性传递插槽内容scopedSlots: {default: props => h('div', `Parent Slot Content: ${props.text}`)}})},components: {MyComponent}
}).$mount('#app')// 最终渲染的结果是:
<div id="app"><div><h1>Hello World</h1><div>Parent Slot Content: Child Slot Text</div></div>
</div>

12 . directives-属性是一个对象,用来设置指令。指令是一种特殊的属性,通过设置指令可以在元素上执行一些自定义的逻辑或者操作。

import Vue from 'vue';Vue.directive('my-directive', {// 指令的生命周期钩子函数bind: function (el, binding, vnode) {// 在绑定时被调用,可以在这里进行初始化设置// el是指令绑定的元素// binding是一个对象,包含了指令的相关信息,如指令参数、修饰符、绑定值等// vnode是指令所在的虚拟DOM节点el.style.color = binding.value;},update: function (el, binding, vnode) {// 在节点更新时被调用,可以在这里对节点进行更新el.style.color = binding.value;}
});var vm = new Vue({el: '#app',render: function (h) {return h('div', {directives: [{name: 'my-directive',value: 'red'}],style: {width: '100px',height: '100px',background: 'yellow'}}, 'Hello, Vue.js!');}
});

ok!搞定!

http://www.lryc.cn/news/329016.html

相关文章:

  • ubuntu23.10配置RUST开发环境
  • Vue性能优化--gZip
  • 蓝桥杯第七届大学B组详解
  • 荣誉 | 人大金仓连续三年入选“金融信创优秀解决方案”
  • 【关于jupyter notebook】一打开就闪退的问题
  • 若依 3.8.7版本springboot前后端分离 整合mabatis plus
  • vue做移动端自适应插件实现rem
  • android 快速实现 图片获取并裁剪(更换头像)
  • 垃圾回收机制--GC 垃圾收集器--JVM调优-面试题
  • Java基础知识总结(29)
  • vue js金额转中文
  • 《QT实用小工具·二》图片文字转base64编码
  • Django安装及第一个项目
  • 专升本-物联网
  • 二叉树的遍历C语言
  • PostgreSQL到Doris的迁移技巧:实时数据同步新选择!
  • 【三维】关于万向节锁的直白解释
  • 程序员的修养 - 变量
  • 判断一个元素是否在可视区域中
  • 【Monero】Wallet RPC | Wallet CLI | 门罗币命令行查询余额、种子、地址等命令方法教程
  • FPGA----ZCU106的petalinux 2019.1使用USB传输数据
  • 备考ICA----Istio实验10---为单个主机配置TLS Istio Ingress Gateway实验
  • 【4】单链表(有虚拟头节点)
  • html第二次作业
  • Android客户端自动化UI自动化airtest从0到1搭建macos+脚本设计demo演示+全网最全最详细保姆级有步骤有图
  • 基于单片机的自动浇灌系统的设计
  • WebStorm 与 VSCode 对比分析
  • git命令-项目使用
  • python安装删除以及pip的使用
  • 7、鸿蒙学习-共享包概述