亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

vue渲染方式render和template的區(qū)別

瀏覽:24日期:2023-01-15 14:54:00

render函數(shù)詳解

Vue中的Render函數(shù)中有一個參數(shù),這個參數(shù)是一個函數(shù)通常我們叫做h。其實這個h叫做createElement。Render函數(shù)將createElement的返回值放到了HTML中

createElement這個函數(shù)中有3個參數(shù)

第一個參數(shù)(必要參數(shù)):主要用于提供DOM的html內(nèi)容,類型可以是字符串、對象或函數(shù)

第二個參數(shù)(類型是對象,可選):用于設(shè)置這個DOM的一些樣式、屬性、傳的組件的參數(shù)、綁定事件之類

第三個參數(shù)(類型是數(shù)組,數(shù)組元素類型是VNode,可選):主要是指該結(jié)點下還有其他結(jié)點,用于設(shè)置分發(fā)的內(nèi)容,包括新增的其他組件。注意,組件樹中的所有VNode必須是唯一的

// @return {VNode}createElement( // {String | Object | Function} // 一個HTML標(biāo)簽字符串,組件選項對象,或者一個返回值類型為String/Object的函數(shù)。該參數(shù)是必須的 ’div’, // {Object} // 一個包含模板相關(guān)屬性的數(shù)據(jù)對象,這樣我們可以在template中使用這些屬性,該參數(shù)是可選的。{ attrs: { name: headingId, href: ’#’+headingId }, style: { color: ’red’, fontSize: ’20px’ }, ’class’: { foo: true, bar: false }, // DOM屬性 domProps: { innerHTML: ’baz’ }, // 組件props props: { myProp: ’bar’ }, // 事件監(jiān)聽基于 ’on’ // 所以不再支持如 ’v-on:keyup.enter’ 修飾語 // 需要手動匹配 KeyCode on: { click: function(event) {event.preventDefault();console.log(111); } } } // {String | Array} // 子節(jié)點(VNodes)由 createElement() 構(gòu)建而成??蛇x參數(shù) // 或簡單的使用字符串來生成的 '文本節(jié)點'。[ ’xxxx’, createElement(’h1’, ’一則頭條’), createElement(MyComponent, { props: { someProp: ’xxx’ } }), this.$slots.default])

什么時候用render函數(shù)?

假設(shè)我們要封裝一套按鈕組件,按鈕有四個樣式(success、error、warning、default)。首先,你可能會想到如下實現(xiàn)

<template><divclass='btn btn-success'v-if='type === ’success’'>{{ text }}</div><divclass='btn btn-danger'v-else-if='type === ’danger’'>{{ text }}</div><divclass='btn btn-warning'v-else-if='type === ’warning’'>{{ text }}</div></template>

雖然我們這樣實現(xiàn)沒有問題,但是如果現(xiàn)在有十幾個樣式的情況下我們就需要寫N多個判斷,如果遇到了這種情況我們就可以選擇使用render函數(shù)。

其實簡單的來說就是template適合簡單的組件封裝,然后render函數(shù)適合復(fù)雜的組件封裝

<script>Vue.component('A-button', { props: { type: {type: String,default: ’default’ }, text: {type: String,default: ’按鈕’ } }, computed: { tag() {switch(this.type) { case’success’: return1; case’danger’: return2; case’warning’: return3; default: return1; } } }, render(h) { returnh(’div’, {class: { btn: true, ’btn-success’: this.type===’success’, ’btn-danger’: this.type===’danger’, ’btn-warning’: this.type===’warning’ },domProps: { //innerText: this.text, },on: { click: this.handleClick } }, this.$slots.default ); }, methods: { handleClick() {console.log(’-----------------------’);console.log(’li’); } } }) letvm=newVue({ el: '#app' })</script>

template與render函數(shù)對比

template----html的方式做渲染render----js的方式做渲染

render(提供)是一種編譯方式render里有一個函數(shù)h,這個h的作用是將單文件組件進行虛擬DOM的創(chuàng)建,然后再通過render進行解析。h就是createElement()方法:createElement(標(biāo)簽名稱,屬性配置,children)

template也是一種編譯方式,但是template最終還是要通過render的方式再次進行編譯。

區(qū)別:1、render渲染方式可以讓我們將js發(fā)揮到極致,因為render的方式其實是通過createElement()進行虛擬DOM的創(chuàng)建。邏輯性比較強,適合復(fù)雜的組件封裝。2、template是類似于html一樣的模板來進行組件的封裝。3、render的性能比template的性能好很多4、render函數(shù)優(yōu)先級大于template

案例一:template和render的方式渲染標(biāo)題標(biāo)簽:

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title></head><body> <div id='app'> <h-title level='1'>標(biāo)題</h-title> <h-title level='2'>標(biāo)題</h-title> <h-title level='3'>標(biāo)題</h-title> </div> <script src='https://unpkg.com/vue/dist/vue.js'></script> <script> Vue.component('h-title',{ /* template渲染 */ // template:` // <div> // <h1 v-if='level==1'><slot></slot></h1>// <h2 v-else-if='level==2'><slot></slot></h2>// <h3 v-else-if='level==3'><slot></slot></h3>// </div> // `, /* render渲染 */ render:function(h){// createElement(標(biāo)簽名稱,屬性配置,children)return h('h'+this.level, { attrs:{ 'data-id':10 } }, // 相當(dāng)于<slot></slot>標(biāo)簽接收 this.$slots.default) }, props:{level:{ type:String} } }); let vm=new Vue({ el:'#app' }); </script></body></html>

案例二:render方式模擬button:

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title> <style> *{margin: 0;padding: 0;} .btn{ width: 80px; line-height: 40px; text-align: center; color:#fff; border-radius: 5px; background-color: #ccc; } .success{background-color: green;} .error{background-color: red;} .info{background-color: pink;} </style></head><body> <div id='app'> <wql-button type='success'>成功</wql-button> <wql-button type='info'>提示</wql-button> <wql-button type='error'>報錯</wql-button> <wql-button>默認</wql-button> </div> <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script> <script> Vue.component('wql-button',{ render:function(h){return h('div',{ class:{ btn:true, success:this.type=='success', error:this.type=='error', info:this.type=='info' }},this.$slots.default); }, props:{type:{ type:String} } }); let vm=new Vue({ el:'#app' }); </script></body></html>

到此這篇關(guān)于vue渲染方式render和template的區(qū)別的文章就介紹到這了,更多相關(guān)vue render template區(qū)別內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 欧美精品一区二区三区免费观看 | 色先锋av资源中文字幕 | 91福利国产在线观看一区二区 | 91日本| 人喾交性专区免费看 | 成人国产精品高清在线观看 | 美国黄色毛片一级 | 欧美视频在线观看免费播放 | 亚洲另类在线视频 | 在线观看色视频 | 国产成人精品亚洲777图片 | www.爱色.com| 国产成人18黄网站免费 | 国产凹凸一区在线观看视频 | 北条麻妃一区二区三区 | 日本一区二区三区高清福利视频 | 日韩亚洲欧美综合 | 蜜桃视频一区二区在线看 | 亚洲青青青网伊人精品 | 亚洲精品午夜在线观看 | 91高清免费视频 | 久久国产高清字幕中文 | 国产超级碰碰在线公开视频 | 久久成人国产 | 日本一级毛片 | 国产精品久久国产三级国不卡顿 | 99久久精品免费看国产情侣 | 免费的看黄网站 | 欧美成人一区二区三区不卡 | 爱爱www在线观看视频高清 | 亚洲欧美日韩中文字幕在线 | 国产精品国产亚洲精品看不卡 | 久久在线观看免费视频 | 91福利一区二区 | 91精品啪在线观看国产日本 | 一本大道无香蕉综合在线 | 国产精品jizz观看 | 91成人在线播放 | 亚洲天堂久久精品成人 | 久久综合图区亚洲综合图区 | 亚洲国产成人最新精品资源 |