詳解vue渲染函數(shù)render的使用
1.什么是render函數(shù)?
vue通過 template 來創(chuàng)建你的 HTML。但是,在特殊情況下,這種寫死的模式無法滿足需求,必須需要js的編程能力。此時,需要用render來創(chuàng)建HTML。
比如如下我想要實(shí)現(xiàn)如下html:
<div id="container"> <h1> <a href="#" rel="external nofollow" rel="external nofollow" > Hello world! </a> </h1> </div>
我們會如下使用:
<!DOCTYPE html>
<html>
<head>
<title>演示Vue</title>
<style>
</style>
</head>
<body>
<div id="container">
<tb-heading :level="1">
<a href="#" rel="external nofollow" rel="external nofollow" >Hello world!</a>
</tb-heading>
</div>
</body>
<script src="./vue.js"></script>
<script type="text/x-template" id="templateId">
<h1 v-if="level === 1">
<slot></slot>
</h1>
<h2 v-else-if="level === 2">
<slot></slot>
</h2>
</script>
<script>
Vue.component('tb-heading', {
template: '#templateId',
props: {
level: {
type: Number,
required: true
}
}
});
new Vue({
el: '#container'
});
</script>
</html>
2.例:
遇到的問題:
在工作中,我創(chuàng)建了一個button組件,又創(chuàng)建了一個button-group組件
button組件較為簡單,就是一個可以輸入type/size/icon等屬性的button

此為渲染后結(jié)果。
然后,創(chuàng)建button-group組件,目標(biāo)結(jié)果為

此處,不僅要在最外層包裹一層div,還要在每個button組件外層包裹一層p標(biāo)簽。此處,就需要使用render函數(shù)了。
既然有了render函數(shù),就不再需要template標(biāo)簽了,vue文件中只需要script標(biāo)簽(該組件style是全局的)
button-group.vue如下
<script>
export default {
name: "XButtonGroup",
props: {
compact: { //自定義的button-group屬性,影響其classname
type: Boolean,
default: true
}
},
render(createElement) {
//此處創(chuàng)建element
},
computed: {
groupClass() {
const className = ["field"]; //通過計算屬性監(jiān)聽compact屬性傳入className
className.push(this.compact ? "has-addons" : "is-grouped");
return className;
}
}
};
</script>
接下來就要看render函數(shù)了。
render函數(shù)中的createElement方法有三個參數(shù)。第一個參數(shù)為外層標(biāo)簽名,第二個為外層標(biāo)簽的屬性對象,第三個為外層標(biāo)簽中的內(nèi)容
所以第一步
render(createElement) {
return createElement(
'div', {
class: this.groupClass
}, '內(nèi)容',
)
}
渲染結(jié)果:
![]()
那怎樣在外層div中渲染button組件呢?
render函數(shù)的第三個參數(shù)除了字符串,還可以傳入VNode的數(shù)組。VNode就是vue中的節(jié)點(diǎn)。
此處,我們通過this.$slots.default獲取所有插入到button-group組件內(nèi)默認(rèn)slot的button節(jié)點(diǎn)
render(createElement) {
return createElement(
'div', {
class: this.groupClass
}, this.$slots.default,
)
},
渲染結(jié)果:

button已經(jīng)正確渲染到了外層div中。但是怎么在每個button外層包裹一層元素呢。createElement會創(chuàng)建新的VNode,而render函數(shù)第三個參數(shù)需要VNode數(shù)組,所以我們需要傳入一個由createElement返回值組成的數(shù)組。
render(createElement) {
//遍歷每一個VNode,用createElement函數(shù)在外層包裹c(diǎn)lass為control的p標(biāo)簽,組成新的VNode數(shù)組
const arry = this.$slots.default.map(VNode => {
return createElement('p', {
class: 'control'
}, [VNode])
})
return createElement(
'div', {
class: this.groupClass
}, arry,
)
},
渲染結(jié)果:

并且根據(jù)button-group的compact屬性可以切換不同的class,生成不同的效果
<x-button-group :compact="true">
<x-button v-for="(item,index) in buttonType" :key="index" :type="item">{{item}}</x-button>
</x-button-group>
<x-button-group :compact="false">
<x-button v-for="(item,index) in buttonType" :key="index" :type="item">{{item}}</x-button>
</x-button-group>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue打包部署到springboot并通過tomcat運(yùn)行的操作方法
這篇文章主要介紹了vue打包部署到springboot并通過tomcat運(yùn)行的操作方法,本文通過實(shí)例圖文并茂的形式給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05
Element 的 el-table 表格實(shí)現(xiàn)單元格合并功能
這篇文章主要介紹了Element 的 el-table 表格實(shí)現(xiàn)單元格合并功能,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-07-07
Flutter部件內(nèi)部狀態(tài)管理小結(jié)之實(shí)現(xiàn)Vue的v-model功能
本文是 Flutter 部件內(nèi)部狀態(tài)管理的小結(jié),從部件的基礎(chǔ)開始,到部件的狀態(tài)管理,并且在過程中實(shí)現(xiàn)一個類似 Vue 的 v-model 的功能,感興趣的朋友跟隨小編一起看看吧2019-06-06
Element中Upload組件上傳功能實(shí)現(xiàn)(圖片和文件的默認(rèn)上傳及自定義上傳)
這篇文章主要介紹了Element中Upload組件上傳功能實(shí)現(xiàn)包括圖片和文件的默認(rèn)上傳及自定義上傳,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
Vee-validate 父組件獲取子組件表單校驗結(jié)果的實(shí)例代碼
vee-validate 是為 Vue.js 量身打造的表單校驗框架,允許您校驗輸入的內(nèi)容并顯示對應(yīng)的錯誤提示信息。這篇文章主要介紹了Vee-validate 父組件獲取子組件表單校驗結(jié)果 ,需要的朋友可以參考下2019-05-05
Vue中金額、日期格式化插件@formatjs/intl的使用及說明
這篇文章主要介紹了Vue中金額、日期格式化插件@formatjs/intl的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Vue使用枚舉類型實(shí)現(xiàn)HTML下拉框步驟詳解
本文分步驟給大家介紹了Vue使用枚舉類型實(shí)現(xiàn)HTML下拉框的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-02-02

