小編給大家分享一下怎么使用Vue中的render函數(shù),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設,平順企業(yè)網(wǎng)站建設,平順品牌網(wǎng)站建設,網(wǎng)站定制,平順網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,平順網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
第一個參數(shù)(必須) - {String | Object | Function}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>render</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app"> <elem></elem> </div> <script> Vue.component('elem', { render: function(createElement) { return createElement('div');//一個HTML標簽字符 /*return createElement({ template: '<div></div>'//組件選項對象 });*/ /*var func = function() { return {template: '<div></div>'} }; return createElement(func());//一個返回HTML標簽字符或組件選項對象的函數(shù)*/ } }); new Vue({ el: '#app' }); </script> </body> </html>
第二個參數(shù)(可選) - {Object}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>render</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app"> <elem></elem> </div> <script> Vue.component('elem', { render: function(createElement) { var self = this; return createElement('div', {//一個包含模板相關屬性的數(shù)據(jù)對象 'class': { foo: true, bar: false }, style: { color: 'red', fontSize: '14px' }, attrs: { id: 'foo' }, domProps: { innerHTML: 'baz' } }); } }); new Vue({ el: '#app' }); </script> </body> </html>
第三個參數(shù)(可選) - {String | Array}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>render</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app"> <elem></elem> </div> <script> Vue.component('elem', { render: function(createElement) { var self = this; // return createElement('div', '文本');//使用字符串生成文本節(jié)點 return createElement('div', [//由createElement函數(shù)構建而成的數(shù)組 createElement('h2', '主標'),//createElement函數(shù)返回VNode對象 createElement('h3', '副標') ]); } }); new Vue({ el: '#app' }); </script> </body> </html>
兩種組件寫法對比
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>render</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app"> <ele></ele> </div> <script> /*Vue.component('ele', { template: '<div id="elem" :class="{show: show}" @click="handleClick">文本</div>', data: function() { return { show: true } }, methods: { handleClick: function() { console.log('clicked!'); } } });*/ Vue.component('ele', { render: function(createElement) { return createElement('div', { 'class': { show: this.show }, attrs: { id: 'elem' }, on: { click: this.handleClick } }, '文本'); }, data: function() { return { show: true } }, methods: { handleClick: function() { console.log('clicked!'); } } }); new Vue({ el: '#app' }); </script> </body> </html>
this.$slots用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>render</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app"> <blog-post> <h2 slot="header"><span>About Me</span></h2> <p>Here's some page content</p> <p slot="footer">Copyright 2016 Evan You</p> <p>If I have some content down here</p> </blog-post> </div> <script> Vue.component('blog-post', { render: function(createElement) { var header = this.$slots.header,//返回由VNode組成的數(shù)組 body = this.$slots.default, footer = this.$slots.footer; return createElement('div', [ createElement('header', header), createElement('main', body), createElement('footer', footer) ]) } }); new Vue({ el: '#app' }); </script> </body> </html>
使用props傳遞數(shù)據(jù)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>render</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app"> <ele :show="show"></ele> <ele :show="!show"></ele> </div> <script> Vue.component('ele', { render: function(createElement) { if (this.show) { return createElement('p', 'true'); } else { return createElement('p', 'false'); } }, props: { show: { type: Boolean, default: false } } }); new Vue({ el: '#app', data: { show: false } }); </script> </body> </html>
VNodes必須唯一
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>render</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <!-- VNode必須唯一 --> <div id="app"> <ele></ele> </div> <script> var child = { render: function(createElement) { return createElement('p', 'text'); } }; /*Vue.component('ele', { render: function(createElement) { var childNode = createElement(child); return createElement('div', [ childNode, childNode//VNodes必須唯一,渲染失敗 ]); } });*/ Vue.component('ele', { render: function(createElement) { return createElement('div', Array.apply(null, { length: 2 }).map(function() { return createElement(child)//正確寫法 }) ); } }); new Vue({ el: '#app' }) </script> </body> </html>
v-model指令
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>render</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app"> <el-input :name="name" @input="val=>name=val"></el-input> <div>你的名字是{{name}}</div> </div> <script> Vue.component('el-input', { render: function(createElement) { var self = this; return createElement('input', { domProps: { value: self.name }, on: { input: function(event) { self.$emit('input', event.target.value); } } }) }, props: { name: String } }); new Vue({ el: '#app', data: { name: 'hdl' } }); </script> </body> </html>
作用域插槽
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>render</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app"> <ele> <template scope="props"> <span>{{props.text}}</span> </template> </ele> </div> <script> Vue.component('ele', { render: function(createElement) { // 相當于<div><slot :text="msg"></slot></div> return createElement('div', [ this.$scopedSlots.default({ text: this.msg }) ]); }, data: function() { return { msg: '來自子組件' } } }); new Vue({ el: '#app' }); </script> </body> </html>
向子組件中傳遞作用域插槽
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>render</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app"> <ele></ele> </div> <script> Vue.component('ele', { render: function(createElement) { return createElement('div', [ createElement('child', { scopedSlots: { default: function(props) { return [ createElement('span', '來自父組件'), createElement('span', props.text) ]; } } }) ]); } }); Vue.component('child', { render: function(createElement) { return createElement('b', this.$scopedSlots.default({text: '我是組件'})); } }); new Vue({ el: '#app' }); </script> </body> </html>
函數(shù)化組件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>render</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app"> <smart-item :data="data"></smart-item> <button @click="change('img')">切換為圖片為組件</button> <button @click="change('video')">切換為視頻為組件</button> <button @click="change('text')">切換為文本組件</button> </div> <script> // 圖片組件選項 var ImgItem = { props: ['data'], render: function(createElement) { return createElement('div', [ createElement('p', '圖片組件'), createElement('img', { attrs: { src: this.data.url } }) ]); } } // 視頻組件 var VideoItem = { props: ['data'], render: function(createElement) { return createElement('div', [ createElement('p', '視頻組件'), createElement('video', { attrs: { src: this.data.url, controls: 'controls', autoplay: 'autoplay' } }) ]); } }; /*純文本組件*/ var TextItem = { props: ['data'], render: function(createElement) { return createElement('div', [ createElement('p', '純文本組件'), createElement('p', this.data.text) ]); } }; Vue.component('smart-item', { functional: true, render: function(createElement, context) { function getComponent() { var data = context.props.data; if (data.type === 'img') return ImgItem; if (data.type === 'video') return VideoItem; return TextItem; } return createElement( getComponent(), { props: { data: context.props.data } }, context.children ) }, props: { data: { type: Object, required: true } } }); new Vue({ el: '#app', data() { return { data: {} } }, methods: { change: function(type) { if (type === 'img') { this.data = { type: 'img', url: '/upload/otherpic76/66772.png' } } else if (type === 'video') { this.data = { type: 'video', url: 'http://vjs.zencdn.net/v/oceans.mp4' } } else if (type === 'text') { this.data = { type: 'text', content: '這是一段純文本' } } } }, created: function() { this.change('img'); } }); </script> </body> </html>
以上是“怎么使用Vue中的render函數(shù)”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當前名稱:怎么使用Vue中的render函數(shù)
網(wǎng)址分享:http://jinyejixie.com/article20/pppojo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、動態(tài)網(wǎng)站、手機網(wǎng)站建設、搜索引擎優(yōu)化、定制網(wǎng)站、營銷型網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)