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

2024-03-31 20:28

本文主要是介绍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!搞定!

这篇关于vue render 函数详解 (配参数详解)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/865256

相关文章

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class