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

什么是render函數(shù)
在vue中我們使用模板HTML語法組建頁面,通過render函數(shù)用js語言來構(gòu)建DOM。
因?yàn)関ue是虛擬DOM,所以在拿到template模板時也要轉(zhuǎn)譯成VNode的函數(shù),而用render函數(shù)構(gòu)建DOM,vue就免去了轉(zhuǎn)譯的過程。
render函數(shù)構(gòu)建DOM需要用到vue提供的工具createElement函數(shù),約定簡寫h。
render函數(shù)的返回值(VNode)
VNode(即:虛擬節(jié)點(diǎn)),也就是我們要渲染的節(jié)點(diǎn)。
template與render
Template適合邏輯簡單,render適合復(fù)雜邏輯。render的性能較高,template性能較低。
當(dāng)存在template與render時,優(yōu)先展示template(render不展示)
<template>
<div>gggg</div>
</template>
...
render() {
return <div>我收到的</div>;
}
簡單的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>
);
}動態(tài)綁定
render() {
const { setting } = this.data;
return (
<div class={this.$style.item}>
<FormCardStyle silhouette={true} type={setting.template} />
</div>
);
}什么時候使用Render
自定義組件的時候。
<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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3通過render函數(shù)實(shí)現(xiàn)菜單下拉框的示例
- vue中的render函數(shù)、h()函數(shù)、函數(shù)式組件詳解
- Vue中render函數(shù)調(diào)用時機(jī)與執(zhí)行細(xì)節(jié)源碼分析
- 簡單談一談Vue中render函數(shù)
- vue中使用render封裝一個select組件
- Vue 2閱讀理解之initRender與callHook組件詳解
- vue語法之render函數(shù)和jsx的基本使用
- vue3中的render函數(shù)里定義插槽和使用插槽
- VUE3中h()函數(shù)和createVNode()函數(shù)的使用解讀
- Vue.js3.2的vnode部分優(yōu)化升級使用示例詳解
- Vue.js之VNode的使用
- vue中?render?函數(shù)功能與原理分析
相關(guān)文章
el-form表單實(shí)現(xiàn)校驗(yàn)的示例代碼
本文主要介紹了el-form表單實(shí)現(xiàn)校驗(yàn)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(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ù)并且修改賦值全部內(nèi)容,需要的朋友可以參考下2022-12-12
基于Vue和ECharts實(shí)現(xiàn)定時更新與倒計時功能的實(shí)戰(zhàn)分享
在前端開發(fā)中,動態(tài)數(shù)據(jù)展示和用戶交互是構(gòu)建現(xiàn)代 Web 應(yīng)用的核心需求之一,在本篇博客中,我們將通過一個簡單的案例,展示如何在 Vue 中結(jié)合 ECharts 實(shí)現(xiàn)一個定時更新和倒計時功能,用于實(shí)時監(jiān)控和數(shù)據(jù)可視化,需要的朋友可以參考下2025-01-01
vue阻止重復(fù)請求實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了vue阻止重復(fù)請求實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Vue實(shí)現(xiàn)鼠標(biāo)懸浮隱藏與顯示圖片效果@mouseenter和@mouseleave事件詳解
在所做的Vue項(xiàng)目中,有時候需要在鼠標(biāo)移動文字框的時候顯示一些詳細(xì)信息,下面這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)鼠標(biāo)懸浮隱藏與顯示圖片效果@mouseenter和@mouseleave事件的相關(guān)資料,需要的朋友可以參考下2022-11-11
Vue3+vantUI3時間組件封裝過程支持選擇年以及年月日時分秒
這篇文章主要介紹了Vue3+vantUI3時間組件封裝過程支持選擇年以及年月日時分秒,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07

