网站平台设计费用,魔法网站小程序开发,广西建设网官网住房和城乡厅官网官方网,做网站业务员如何跟客户沟通vue render 函数详解 (配参数详解) 在 Vue 3 中#xff0c;render 函数被用来代替 Vue 2 中的模板语法。 它接收一个 h 函数#xff08;或者是 createElement 函数的别名#xff09;#xff0c;并且返回一个虚拟 DOM。
render 函数的语法结构如下#xff1a;
render(h) …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 vueconst MyComponent defineComponent({render(h) {return h(div, Hello, Component!)}
})以下是h函数的第二个参数属性的详解(class、style、attrs、props、on、nativeOn、directives、slot、key、ref、scopedSlots)
class - 设置元素的CSS类名可以使用字符串或对象。对象的键是类名值是一个布尔值用于动态地添加或移除类名。
h(div, {class: red
})
// 创建div classred/divh(div, {class: {red: true,bold: false}
})
// 创建div classred/divstyle - 设置元素的内联样式可以使用字符串、对象或数组。
h(div, {style: color: red;
})
// 创建div stylecolor: red;/divh(div, {style: {color: red,fontSize: 14px}
})
// 创建div stylecolor: red; font-size: 14px;/divh(div, {style: [{ color: red },{ fontSize: 14px }]
})
// 创建div stylecolor: red; font-size: 14px;/divattrs - 设置元素的属性可以使用对象或数组。
h(input, {attrs: {type: text,placeholder: Enter text}
})
// 创建input typetext placeholderEnter texth(div, {attrs: [{ id: my-id },{ data-custom-attr: value }]
})
// 创建div idmy-id data-custom-attrvalue/divprops - 设置元素的DOM属性与attrs类似但是props适用于组件的props。
h(my-component, {props: {message: Hello world}
})
// 创建my-component messageHello world/my-componenton - 绑定事件处理函数可以使用对象或数组。
h(button, {on: {click: handleClick}
})
// 创建button clickhandleClick/buttonh(div, {on: [{ click: handleClick },{ mouseover: handleMouseOver }]
})
// 创建div clickhandleClick mouseoverhandleMouseOver/divnativeOn - 属性用于指定元素的原生事件监听器即直接绑定到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
});
domProps - 设置元素的DOM属性比如innerHTML、textContent等。
h(span, {domProps: {textContent: Hello}
})
// 创建spanHello/spanh(div, {domProps: {innerHTML: pParagraph/p}
})
// 创建divpParagraph/p/divkey - 用于VNode的唯一标识用于在列表渲染中进行优化。
h(div, {key: my-key,class: red
})
// 创建div keymy-key classred/divref - 用于给元素或组件设置一个引用标识以便通过$refs属性访问。
h(input, {ref: myInput,attrs: {type: text}
})
// 创建input refmyInput typetexth(my-component, {ref: myComponent
})
// 创建my-component refmyComponent/my-componentslot - 用于分发内容到组件的插槽。
h(my-component, [h(div, {slot: header}, Header content),h(div, {slot: footer}, Footer content)
])
// 创建my-componentdiv slotheaderHeader content/divdiv slotfooterFooter content/div/my-componentscopedSlots - 属性是一个包含插槽信息的对象。它的每个键是插槽的名称对应的值是一个函数或者一个具有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 idappdivh1Hello World/h1divParent 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搞定