在Vue3中,render函数被用来代替 Vue 2 中的模板语法。它接收一个 h 函数(或者是 `createElement` 函数的别名),并且返回一个虚拟 DOM。
render函数的语法结构如下:
javascript
render(h) {
return h('div', { class: 'container' }, 'Hello, World!')
}
使用 h
函数创建了一个 div
元素,并设置了 class
属性,并且在 div
元素中添加了文本内容 。
h函数的使用方式如下:
scss
h(tag, data, children)
1)tag:表示要创建的元素的标签名,可以是字符串或者是组件选项对象。
2)data:表示要添加到元素的属性、事件等;可以包含普通 HTML 属性、DOM 属性、事件、样式等
3)children:表示要作为子节点添加到元素中的内容。
php
render(h) {
return h('div', {
class: 'container',
style: { color: 'red' },
on: {
click: () => {
console.log('Clicked!')
}
}
}, 'Hello, World!')
}
除了使用原生的 HTML 标签,我们还可以使用组件选项对象来创建组件。例如:
javascript
const MyComponent = {
render(h) {
return h('div', 'Hello, Component!')
}
}
render(h) {
return h(MyComponent)
}
h函数的第二个参数属性的详解
1.class - 设置元素的CSS类名,可以使用字符串或对象。对象的键是类名,值是一个布尔值,用于动态地添加或移除类名。
php
h('div', {
class: 'red'
})
// 创建<div class="red"></div>
h('div', {
class: {
red: true,
bold: false
}
})
// 创建<div class="red"></div>
2.style - 设置元素的内联样式,可以使用字符串、对象或数组。
css
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>
3.attrs - 设置元素的属性,可以使用对象或数组。
bash
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>
4.props - 设置元素的DOM属性,与attrs类似,但是props适用于组件的props。
perl
h('my-component', {
props: {
message: 'Hello world'
}
})
// 创建<my-component message="Hello world"></my-component>
5.on - 绑定事件处理函数,可以使用对象或数组。
css
h('button', {
on: {
click: handleClick
}
})
// 创建<button @click="handleClick"></button>
h('div', {
on: [
{ click: handleClick },
{ mouseover: handleMouseOver }
]
})
// 创建<div @click="handleClick" @mouseover="handleMouseOver"></div>
6.nativeOn - 属性用于指定元素的原生事件监听器,即直接绑定到DOM元素上的事件,而不是绑定到组件上的自定义事件。
javascript
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'
});
7.domProps - 设置元素的DOM属性,比如innerHTML、textContent等。
css
h('span', {
domProps: {
textContent: 'Hello'
}
})
// 创建<span>Hello</span>
h('div', {
domProps: {
innerHTML: '<p>Paragraph</p>'
}
})
// 创建<div><p>Paragraph</p></div>
8.key - 用于VNode的唯一标识,用于在列表渲染中进行优化。
vbnet
h('div', {
key: 'my-key',
class: 'red'
})
// 创建<div key="my-key" class="red"></div>
9.ref - 用于给元素或组件设置一个引用标识,以便通过$refs属性访问。
perl
h('input', {
ref: 'myInput',
attrs: {
type: 'text'
}
})
// 创建<input ref="myInput" type="text">
h('my-component', {
ref: 'myComponent'
})
// 创建<my-component ref="myComponent"></my-component>
10.slot - 用于分发内容到组件的插槽。
css
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>
11.scopedSlots - 属性是一个包含插槽信息的对象。它的每个键是插槽的名称,对应的值是一个函数或者一个具有render函数的对象。
javascript
// 示例组件
const MyComponent = {
render(h) {
return h('div', [
h('h1', 'Hello World'),
h('slot', {
// 插槽名称为default
scopedSlots: {
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-属性是一个对象,用来设置指令。指令是一种特殊的属性,通过设置指令可以在元素上执行一些自定义的逻辑或者操作。
php
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!');
}
});