Vue3中的h函數(shù)及使用小結(jié)
參考資料:專欄目錄請點(diǎn)擊
簡介
- 眾所周知,vue內(nèi)部構(gòu)建的其實(shí)是虛擬DOM,而虛擬DOM是由虛擬節(jié)點(diǎn)生成的,實(shí)質(zhì)上虛擬節(jié)點(diǎn)也就是一個(gè)js對象
- 事實(shí)上,我們在vue中寫的template,最終也是經(jīng)過渲染函數(shù)生成對應(yīng)的VNode
- 而h函數(shù)就是用來生成VNode的一個(gè)函數(shù),他的全名叫做createVNode
簡單使用
參數(shù)
他一共跟三個(gè)參數(shù)
第一個(gè)參數(shù)
- 是一個(gè)字符串,他是必須的
- 這個(gè)字符串可以是 html標(biāo)簽名,一個(gè)組件、一個(gè)異步的組件或者是函數(shù)組件
第二個(gè)參數(shù)
- 是一個(gè)對象,可選的
- 與attribute、prop和事件相對應(yīng)的對象
第三個(gè)參數(shù)
- 可以是字符串、數(shù)組或者是一個(gè)對象
- 他是VNodes,使用h函數(shù)來進(jìn)行創(chuàng)建
使用
<script> import { h } from 'vue' export default { setup() { return () => h("h2", null, "Hello World") } } </script>
渲染效果如下
當(dāng)然我們還可以使用rener函數(shù)進(jìn)行渲染
<script> import { h } from 'vue' export default { render() { return h("h2", null, "Hello World") } } </script>
計(jì)數(shù)器
<script> import { h } from 'vue' export default { data() { return { counter: 0 } }, render() { return h("div", null, [ h("h2", null, "計(jì)數(shù)器"), h("h3", null, `計(jì)數(shù)${this.counter}`), h("button", { onClick: () => this.counter++ },"點(diǎn)一下") ]) } } </script>
渲染如下
進(jìn)階使用
函數(shù)組件
我們先寫一個(gè)組件HelloWorld.vue
<script setup lang="ts"> import { ref } from 'vue'; const param = ref("Hello World") </script> <template> <h2>{{ param }}</h2> </template> <style scoped lang="less"></style>
然后,我們在h函數(shù)中引入這個(gè)組件,他就會被渲染
<script> import { h } from 'vue' import HelloWorld from './HelloWorld.vue' export default { data() { return { counter: 0 } }, render() { return h("div", null, [h(HelloWorld)]) } } </script>
插槽
h函數(shù)同樣支持插槽,我們把HelloWorld組件改成一個(gè)插槽組件
HelloWorld.vue
<script setup lang="ts"> import { ref } from 'vue'; const param = ref("Hello World") </script> <template> <h2>{{ param }}</h2> <slot></slot> </template> <style scoped lang="less"></style>
index.ts
<script> import { h } from 'vue' import HelloWorld from './HelloWorld.vue' export default { data() { return { counter: 0 } }, render() { return h("div", null, [h(HelloWorld, {}, [h("div", null, "Hello Slot")])]) } } </script>
最終渲染如下
到此這篇關(guān)于Vue3中的h函數(shù)及使用小結(jié)的文章就介紹到這了,更多相關(guān)Vue3 h函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Vue實(shí)現(xiàn)帶拖動(dòng)和播放功能的時(shí)間軸
這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)帶拖動(dòng)和播放功能的時(shí)間軸,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03Vue實(shí)現(xiàn)各種類型文件的預(yù)覽功能
這篇文章主要介紹了如何在Vue3中使用aceEditor插件和vue-ipynb插件實(shí)現(xiàn)不同類型的文件預(yù)覽,包括txt、md、json、pkl、mps、py、ipynb、doc、docx、pdf、xlsx、csv等文件,需要的朋友可以參考下2025-03-03說說如何在Vue.js中實(shí)現(xiàn)數(shù)字輸入組件的方法
這篇文章主要介紹了說說如何在Vue.js中實(shí)現(xiàn)數(shù)字輸入組件的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01基于Vue實(shí)現(xiàn)關(guān)鍵詞實(shí)時(shí)搜索高亮顯示關(guān)鍵詞
這篇文章主要介紹了基于Vue實(shí)現(xiàn)關(guān)鍵詞實(shí)時(shí)搜索高亮顯示關(guān)鍵詞,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07vue3 el-form-item如何自定義label標(biāo)簽內(nèi)容
這篇文章主要介紹了vue3 el-form-item如何自定義label標(biāo)簽內(nèi)容問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue項(xiàng)目之前端CryptoJS加密、解密代碼示例
在Vue項(xiàng)目中集成CryptoJS進(jìn)行數(shù)據(jù)加密,首先需要通過npm安裝CryptoJS安裝包,然后在項(xiàng)目文件中引入CryptoJS,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11關(guān)于Vue-extend和VueComponent問題小結(jié)
這篇文章主要介紹了Vue-extend和VueComponent問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08