Vue render函數(shù)使用詳細(xì)講解
Dom
在瀏覽器中通過(guò)js來(lái)操作DOM的操作性能很差,于是虛擬Dom應(yīng)運(yùn)而生。虛擬Dom就是在js中模擬DOM對(duì)象樹來(lái)優(yōu)化DOM操作的一種技術(shù)或思路。
虛擬DOM并不是真正意義上的DOM,它作為一個(gè)輕量級(jí)的JavaScript對(duì)象,在狀態(tài)發(fā)生變化時(shí),會(huì)進(jìn)行Diff運(yùn)算,來(lái)更新發(fā)生變化的DOM,對(duì)于未發(fā)生變化的DOM節(jié)點(diǎn),不予操作,由于不是全部重繪,大大提高更新渲染性能。

什么是render函數(shù)
在vue中我們使用模板HTML語(yǔ)法組建頁(yè)面,通過(guò)render函數(shù)用js語(yǔ)言來(lái)構(gòu)建DOM。
因?yàn)関ue是虛擬DOM,所以在拿到template模板時(shí)也要轉(zhuǎn)譯成VNode的函數(shù),而用render函數(shù)構(gòu)建DOM,vue就免去了轉(zhuǎn)譯的過(guò)程。
render函數(shù)構(gòu)建DOM需要用到vue提供的工具createElement函數(shù),約定簡(jiǎn)寫h。
render函數(shù)的返回值(VNode)
VNode(即:虛擬節(jié)點(diǎn)),也就是我們要渲染的節(jié)點(diǎn)。
template與render
Template適合邏輯簡(jiǎn)單,render適合復(fù)雜邏輯。render的性能較高,template性能較低。
當(dāng)存在template與render時(shí),優(yōu)先展示template(render不展示)
<template>
<div>gggg</div>
</template>
...
render() {
return <div>我收到的</div>;
}
簡(jiǎn)單的render函數(shù)
render:(h) => {
return h('div',{
//給div綁定value屬性
props: {
value:''
},
//給div綁定樣式
staticStyle:{
width:'30px'
},
//給div綁定點(diǎn)擊事件
on: {
click: () => {
console.log('點(diǎn)擊事件')
}
},
})
} ts-vue的寫法
render() {
return this.$createElement('div', {
on: {
...this.$listeners,
click: event => {
console.log(event);
},
},
props: this.props || {value:''},
attrs: {
businessId: this.field.pkId,
},
style: {
width:'30px'
}
});
} render() {
return (
<thead>
<tr class="ant-table-thead--extra">
{this.title}
</tr>
{this.$slots.default}
</thead>
);
}循環(huán)構(gòu)建:
render() {
return (
<ACheckboxGroup
class="checkbox-list"
value={this.value.map(item => item.id)}
>
{this.dataList.map(item => (
<ACheckbox
key={item.id}
checked={this.checked(item)}
value={item.id}
onChange={$event => this.handleChange($event, item)}
>
{item.name}
</ACheckbox>
))}
</ACheckboxGroup>
);
}分開定義引用:
render() {
const triggerAction = (
<div class="button-list-content--item">
<span>{this.$t(TriggerActionNameEnum[this.value.triggerAction])}</span>
</div>
);
const formName = (
<div class="button-list-content--item">
<span>{this.value?.triggerData?.formName || ''}</span>
</div>
);
const updateFields = (
<div class="button-list-content--item">
<span class="button-list-content--field" title={this.updateFieldName}>
{this.updateFieldName}
</span>
</div>
);
return this.value.triggerAction === TriggerActionEnum.UPDATE ? (
<div>
{triggerAction}
{updateFields}
</div>
) : (
<div>
{triggerAction}
{formName}
</div>
);
}動(dòng)態(tài)綁定
render() {
const { setting } = this.data;
return (
<div class={this.$style.item}>
<FormCardStyle silhouette={true} type={setting.template} />
</div>
);
}什么時(shí)候使用Render
自定義組件的時(shí)候。
<script>
import { Component, Prop, Vue } from 'vue-property-decorator';
@Component()
class Wrap extends Vue {
render() {
return <div>自定義組件A</div>
}
}
@Component()
export default class FormTableActionBar extends Vue {
...
render() {
return (
<div>
<p>下面是自定義組件A</p>
<Wrap></Wrap>
</div>
)
}
}
</script>到此這篇關(guān)于Vue render函數(shù)使用詳細(xì)講解的文章就介紹到這了,更多相關(guān)Vue render函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3通過(guò)render函數(shù)實(shí)現(xiàn)菜單下拉框的示例
- vue中的render函數(shù)、h()函數(shù)、函數(shù)式組件詳解
- Vue中render函數(shù)調(diào)用時(shí)機(jī)與執(zhí)行細(xì)節(jié)源碼分析
- 簡(jiǎn)單談一談Vue中render函數(shù)
- vue中使用render封裝一個(gè)select組件
- Vue 2閱讀理解之initRender與callHook組件詳解
- vue語(yǔ)法之render函數(shù)和jsx的基本使用
- vue3中的render函數(shù)里定義插槽和使用插槽
- VUE3中h()函數(shù)和createVNode()函數(shù)的使用解讀
- Vue.js3.2的vnode部分優(yōu)化升級(jí)使用示例詳解
- Vue.js之VNode的使用
- vue中?render?函數(shù)功能與原理分析
相關(guān)文章
el-form表單實(shí)現(xiàn)校驗(yàn)的示例代碼
本文主要介紹了el-form表單實(shí)現(xiàn)校驗(yàn)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
vue3中如何使用ref和reactive定義和修改響應(yīng)式數(shù)據(jù)(最新推薦)
這篇文章主要介紹了vue3中如何使用ref和reactive定義和修改響應(yīng)式數(shù)據(jù),這里就是vue3中setup組合式api中如何定義響應(yīng)式數(shù)據(jù)并且修改賦值全部?jī)?nèi)容,需要的朋友可以參考下2022-12-12
基于Vue和ECharts實(shí)現(xiàn)定時(shí)更新與倒計(jì)時(shí)功能的實(shí)戰(zhàn)分享
在前端開發(fā)中,動(dòng)態(tài)數(shù)據(jù)展示和用戶交互是構(gòu)建現(xiàn)代 Web 應(yīng)用的核心需求之一,在本篇博客中,我們將通過(guò)一個(gè)簡(jiǎn)單的案例,展示如何在 Vue 中結(jié)合 ECharts 實(shí)現(xiàn)一個(gè)定時(shí)更新和倒計(jì)時(shí)功能,用于實(shí)時(shí)監(jiān)控和數(shù)據(jù)可視化,需要的朋友可以參考下2025-01-01
vue阻止重復(fù)請(qǐng)求實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了vue阻止重復(fù)請(qǐng)求實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Vue實(shí)現(xiàn)鼠標(biāo)懸浮隱藏與顯示圖片效果@mouseenter和@mouseleave事件詳解
在所做的Vue項(xiàng)目中,有時(shí)候需要在鼠標(biāo)移動(dòng)文字框的時(shí)候顯示一些詳細(xì)信息,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)鼠標(biāo)懸浮隱藏與顯示圖片效果@mouseenter和@mouseleave事件的相關(guān)資料,需要的朋友可以參考下2022-11-11
vue實(shí)現(xiàn)實(shí)時(shí)搜索顯示功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)實(shí)時(shí)搜索顯示功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Vue3+vantUI3時(shí)間組件封裝過(guò)程支持選擇年以及年月日時(shí)分秒
這篇文章主要介紹了Vue3+vantUI3時(shí)間組件封裝過(guò)程支持選擇年以及年月日時(shí)分秒,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07

