Vue中render方法的使用詳解
更新時間:2018年01月26日 15:30:29 作者:HellowWorldZz
這篇文章主要為大家詳細介紹了Vue中render方法的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
先說一下對官網(wǎng)上demo的個人理解:
<!DOCTYPE html>
<html>
<head>
<title>Vue的render方法說明</title>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<child :level="1">
hello world
</child>
</div>
<script type="text/x-template" id="anchored-heading-template">
<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 type="text/javascript">
Vue.component('child', {
template: '#anchored-heading-template',
props: {
level: {
type: Number,
required: true
}
}
});
new Vue({
el: "#app"
})
</script>
</body>
</html>
雖然使用template定義組件的方法非常的直觀,但是這樣會造成代碼過長。可以使用render的方法
<!DOCTYPE html>
<html>
<head>
<title>Vue的render方法說明</title>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<child :level="1">
hello world
</child>
</div>
<script type="text/javascript">
Vue.component('child', {
render:function (createElement) {
var body=this.$slots.default;
//this.$slots返回了一個組件分發(fā)下來的元素和內(nèi)容
//this.$slots.default返回了具名的內(nèi)容
return createElement(
'h'+this.level,
//this.level是利用v-bind注入到組件中的level
body
)
},
//因為vue中組件父組件無法向子組件注入內(nèi)容。所以我們需要通過
//v-bind定義一個key,value向子組件注入內(nèi)容。所要接收的值也需要在定義組件時的props屬性中的定義一下
props:{
level:{
}
}
});
new Vue({
el: "#app"
})
</script>
</body>
</html>
下面是一個slot具名分發(fā)的demo:介紹了creatElement的用法:
<!DOCTYPE html>
<html>
<head>
<title>Vue的render方法說明</title>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<child>
<p slot="header">this is header</p>
<p slot="center">this is center</p>
<p slot="footer">this is footer</p>
</child>
</div>
<script type="text/javascript">
Vue.component('child', {
render: function (createElement) {
var header=this.$slots.header;
var center=this.$slots.center;
var footer=this.$slots.footer;
//createElement第一個參數(shù)是標簽名,第二個參數(shù)是值
return createElement('div',[
createElement('div', header),
createElement('div', center),
createElement('div', footer),
])
}
});
new Vue({
el: "#app"
})
</script>
</body>
</html>
所創(chuàng)建的組件的demo結果如下:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 基于vue2.0動態(tài)組件及render詳解
- vue iview組件表格 render函數(shù)的使用方法詳解
- Vue中render函數(shù)的使用方法
- 詳解vue渲染函數(shù)render的使用
- 淺談vue的iview列表table render函數(shù)設置DOM屬性值的方法
- 如何理解Vue的render函數(shù)的具體用法
- vue Render中slots的使用的實例代碼
- 深入理解vue Render函數(shù)
- vue深入解析之render function code詳解
- 了解VUE的render函數(shù)的使用
- Vue2.x中的Render函數(shù)詳解
- Vue.js render方法使用詳解
- Vue render深入開發(fā)講解
相關文章
vue.js的vue-cli腳手架中使用百度地圖API的實例
今天小編就為大家分享一篇關于vue.js的vue-cli腳手架中使用百度地圖API的實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
vue-router實現(xiàn)tab標簽頁(單頁面)詳解
這篇文章主要為大家詳細介紹了vue-router實現(xiàn)tab標簽頁的相關方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Vue使用$attrs實現(xiàn)爺爺直接向孫組件傳遞數(shù)據(jù)
這篇文章主要為大家詳細介紹了Vue如何使用$attrs實現(xiàn)爺爺直接向孫組件傳遞數(shù)據(jù),文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2024-02-02
VUE3中h()函數(shù)和createVNode()函數(shù)的使用解讀
這篇文章主要介紹了VUE3中h()函數(shù)和createVNode()函數(shù)的使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
利用Vue3和element-plus實現(xiàn)圖片上傳組件
element-plus提供了uploader組件,但是不好定制化。所以本文將利用Vue3和element-plus實現(xiàn)一個圖片上傳的組件,感興趣的可以了解一下2022-03-03

