詳解vue渲染函數(shù)render的使用
1.什么是render函數(shù)?
vue通過(guò) template 來(lái)創(chuàng)建你的 HTML。但是,在特殊情況下,這種寫(xiě)死的模式無(wú)法滿(mǎn)足需求,必須需要js的編程能力。此時(shí),需要用render來(lái)創(chuàng)建HTML。
比如如下我想要實(shí)現(xiàn)如下html:
<div id="container"> <h1> <a href="#" rel="external nofollow" rel="external nofollow" > Hello world! </a> </h1> </div>
我們會(huì)如下使用:
<!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.例:
遇到的問(wèn)題:
在工作中,我創(chuàng)建了一個(gè)button組件,又創(chuàng)建了一個(gè)button-group組件
button組件較為簡(jiǎn)單,就是一個(gè)可以輸入type/size/icon等屬性的button

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

此處,不僅要在最外層包裹一層div,還要在每個(gè)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"]; //通過(guò)計(jì)算屬性監(jiān)聽(tīng)compact屬性傳入className
className.push(this.compact ? "has-addons" : "is-grouped");
return className;
}
}
};
</script>
接下來(lái)就要看render函數(shù)了。
render函數(shù)中的createElement方法有三個(gè)參數(shù)。第一個(gè)參數(shù)為外層標(biāo)簽名,第二個(gè)為外層標(biāo)簽的屬性對(duì)象,第三個(gè)為外層標(biāo)簽中的內(nèi)容
所以第一步
render(createElement) {
return createElement(
'div', {
class: this.groupClass
}, '內(nèi)容',
)
}
渲染結(jié)果:
![]()
那怎樣在外層div中渲染button組件呢?
render函數(shù)的第三個(gè)參數(shù)除了字符串,還可以傳入VNode的數(shù)組。VNode就是vue中的節(jié)點(diǎn)。
此處,我們通過(guò)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中。但是怎么在每個(gè)button外層包裹一層元素呢。createElement會(huì)創(chuàng)建新的VNode,而render函數(shù)第三個(gè)參數(shù)需要VNode數(shù)組,所以我們需要傳入一個(gè)由createElement返回值組成的數(shù)組。
render(createElement) {
//遍歷每一個(gè)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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue輸入框狀態(tài)切換&自動(dòng)獲取輸入框焦點(diǎn)的實(shí)現(xiàn)方法
這篇文章主要介紹了Vue輸入框狀態(tài)切換&自動(dòng)獲取輸入框焦點(diǎn)的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
vue3響應(yīng)式Object代理對(duì)象的讀取示例詳解
這篇文章主要為大家介紹了vue3響應(yīng)式Object代理對(duì)象的讀取示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Vue.js實(shí)現(xiàn)實(shí)例搜索應(yīng)用功能詳細(xì)代碼
本文給大家分享Vue.js實(shí)現(xiàn)實(shí)例搜索應(yīng)用功能詳細(xì)代碼,非常不錯(cuò),感興趣的朋友參考下吧2017-08-08
vue實(shí)現(xiàn)將一維數(shù)組變換為三維數(shù)組
這篇文章主要為大家詳細(xì)介紹了vue如何實(shí)現(xiàn)將一維數(shù)組變換為三維數(shù)組,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
使用 Element UI Table 的 slot-scope方法
這篇文章主要介紹了使用 Element UI Table 的 slot-scope方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
vue實(shí)現(xiàn)的請(qǐng)求服務(wù)器端API接口示例
這篇文章主要介紹了vue實(shí)現(xiàn)的請(qǐng)求服務(wù)器端API接口,結(jié)合實(shí)例形式分析了vue針對(duì)post、get、patch、put等請(qǐng)求的封裝與調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2019-05-05

