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

相关文章

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

Java Thread中join方法使用举例详解

《JavaThread中join方法使用举例详解》JavaThread中join()方法主要是让调用改方法的thread完成run方法里面的东西后,在执行join()方法后面的代码,这篇文章主要介绍... 目录前言1.join()方法的定义和作用2.join()方法的三个重载版本3.join()方法的工作原

Spring AI使用tool Calling和MCP的示例详解

《SpringAI使用toolCalling和MCP的示例详解》SpringAI1.0.0.M6引入ToolCalling与MCP协议,提升AI与工具交互的扩展性与标准化,支持信息检索、行动执行等... 目录深入探索 Spring AI聊天接口示例Function CallingMCPSTDIOSSE结束语

C语言进阶(预处理命令详解)

《C语言进阶(预处理命令详解)》文章讲解了宏定义规范、头文件包含方式及条件编译应用,强调带参宏需加括号避免计算错误,头文件应声明函数原型以便主函数调用,条件编译通过宏定义控制代码编译,适用于测试与模块... 目录1.宏定义1.1不带参宏1.2带参宏2.头文件的包含2.1头文件中的内容2.2工程结构3.条件编

PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例

《PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例》词嵌入解决NLP维度灾难,捕捉语义关系,PyTorch的nn.Embedding模块提供灵活实现,支持参数配置、预训练及变长... 目录一、词嵌入(Word Embedding)简介为什么需要词嵌入?二、PyTorch中的nn.Em

Python Web框架Flask、Streamlit、FastAPI示例详解

《PythonWeb框架Flask、Streamlit、FastAPI示例详解》本文对比分析了Flask、Streamlit和FastAPI三大PythonWeb框架:Flask轻量灵活适合传统应用... 目录概述Flask详解Flask简介安装和基础配置核心概念路由和视图模板系统数据库集成实际示例Stre

Spring Bean初始化及@PostConstruc执行顺序示例详解

《SpringBean初始化及@PostConstruc执行顺序示例详解》本文给大家介绍SpringBean初始化及@PostConstruc执行顺序,本文通过实例代码给大家介绍的非常详细,对大家的... 目录1. Bean初始化执行顺序2. 成员变量初始化顺序2.1 普通Java类(非Spring环境)(

Java Spring的依赖注入理解及@Autowired用法示例详解

《JavaSpring的依赖注入理解及@Autowired用法示例详解》文章介绍了Spring依赖注入(DI)的概念、三种实现方式(构造器、Setter、字段注入),区分了@Autowired(注入... 目录一、什么是依赖注入(DI)?1. 定义2. 举个例子二、依赖注入的几种方式1. 构造器注入(Con