Vue中render函數(shù)的使用方法
render函數(shù)
vue通過 template 來創(chuàng)建你的 HTML。但是,在特殊情況下,這種寫死的模式無法滿足需求,必須需要js的編程能力。此時,需要用render來創(chuàng)建HTML。
什么情況下適合使用render函數(shù)
在一次封裝一套通用按鈕組件的工作中,按鈕有四個樣式(default success error )。首先,你可能會想到如下實現(xiàn)
<div v-if="type === 'success'">success</div> <div v-else-if="type === 'error'">error</div> <div v-else-if="type === 'warm'">warm</div> <div v-else>default</div>
這樣寫在按鈕樣式少的時候完全沒有問題,但是試想,如果需要的按鈕樣式有十多個,按鈕內(nèi)的文字根據(jù)實際情況而定(如success按鈕內(nèi)的文字可能是OK、GOOD等等)。那么template寫死的方式就顯得很無力了。遇上類似這樣的情況,使用render函數(shù)可以說最優(yōu)選擇了。
根據(jù)實際情況改寫按鈕組件
首先render函數(shù)生成的內(nèi)容相當(dāng)于template的內(nèi)容,故使用render函數(shù)時,在.vue文件中需要先把template標(biāo)簽去掉。只保留邏輯層。
export default {
render(h) {
return h('div',{
'class': {
btn: true,
success: this.type === 'success',
error: this.type === 'error',
warm: this.type === 'warm',
default: this.type === 'default'
},
domProps: {
innerHTML: this.$slots.default[0].text
},
on: {
click: this.clickHandle
}
})
},
methods: {
clickHandle() {
// dosomething
}
},
props: {
type: {
type: String,
default: 'default'
},
text: {
type: String,
default: 'default'
}
}
};
根據(jù)組件化思維,能抽象出來的東西絕不寫死在邏輯上。這里的clickHandle函數(shù)可以根據(jù)按鈕的type類型觸發(fā)不同的邏輯,就不多敘述了。
然后在父組件調(diào)用
<btn
v-for="(btn, index) in testData"
:type="btn.type"
:text="btn.text"
:key="index">{{btn.text}}
</btn>
使用jsx
是的,要記住每個參數(shù)的類型同用法,按序傳參實在是太麻煩了。那么其實可以用jsx來優(yōu)化這個繁瑣的過程。
return (
<div
class={{
btn: true,
success: this.type === 'success',
error: this.type === 'error',
warm: this.type === 'warm',
default: this.type === 'default'
}}
onClick={this.clickHandle}>
{this.$slots.default[0].text}
</div>
)
示例二:
在遇到寫類似的組件的時候需要寫很多很長的代碼,出于簡潔(懶惰使人進步)的角度來說,我們應(yīng)該找到更合適的方法來實現(xiàn)該效果。
<body>
<div id="app">
<mycomment :level="2">
這是h2元素
</mycomment>
</div>
</body>
<script type="text/x-template" id="is">
<div>
<h1 v-if="level === 1">
<slot></slot>
</h1>
<h2 v-if="level === 2">
<slot></slot>
</h2>
<h3 v-if="level === 3">
<slot></slot>
</h3>
<h4 v-if="level === 4">
<slot></slot>
</h4>
<h5 v-if="level === 5">
<slot></slot>
</h5>
<h6 v-if="level === 6">
<slot></slot>
</h6>
</div>
</script>
<script>
Vue.component('mycomment',{
template:'#is',
props:{
level:{
type:Number,
required:true,
}
}
})
var app =new Vue({
el:'#app',
})
</script>
這時候Render 函數(shù)就很好的解決了這個問題,先來簡單一點額例子,算是有基本的骨架了
<body>
<div id="app">
<render-teample :level="4">
render function
</render-teample>
</div>
</body>
<script>
Vue.component('render-teample',{
render:function(createElement){
return createElement(
'h'+this.level,
this.$slots.default
)
},
props: {
level: {
type: Number,
required: true
}
}
var app=new Vue({
el:"#app",
});
</script>
然后進一步給你的組件加入你想要的樣式需要事件,變得有血有肉
<body>
<div id="app">
<render-teample :level="4" >
<div class="jah" slot="myslot">render function</div>
</render-teample>
</div>
</body>
<script>
Vue.component('render-teample',{
render:function(createElement){
return createElement(
'h'+this.level,
{
'class':{
show:true,
hide:false,
},
style:{
width:'200px',
height:'400px',
background:'red',
},
attrs:{
name:'h-ex',
id:'h-id'
},
props:{
myprops:true,
},
on: {
click: function(event){
alert(this.num)
}
},
nativeOn:{
click:function(event) {
alert(1111)
}
}
},
[
this.$slots.myslot,
createElement('div',{
domProps:{
innerHTML:'holle render'
}
})
]
)
},
props: {
level: {
type: Number,
required: true
}
}
});
var app=new Vue({
el:"#app",
data:{
num:110
}
});
</script>
注意:約束組件中 VNodes 必須是唯一的。
直接把所有元素寫在一個createElement()下是很痛苦的,不利于維護。
所以通常會
var com1= createElement('p','item1');var
com2= createElement('p','item1');
可以使用return createElement('div',[com1,com2])
這種情況是禁止的return createElement('div',[com1,com1])
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue 設(shè)置 input 為不可以編輯的實現(xiàn)方法
今天小編就為大家分享一篇vue 設(shè)置 input 為不可以編輯的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
Vue3實現(xiàn)富文本內(nèi)容導(dǎo)出為Word文檔
這篇文章主要為大家詳細(xì)介紹了Vue3如何通過純前端方案將富文本內(nèi)容直接導(dǎo)出為符合中文排版規(guī)范的 Word 文檔,有需要的小伙伴可以參考下2025-03-03
Vant?如何修改van-collapse-item右側(cè)圖標(biāo)
這篇文章主要介紹了Vant?如何修改van-collapse-item右側(cè)圖標(biāo),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04

