国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue中render函數(shù)的使用方法

2019-11-19 14:26:17
字體:
供稿:網(wǎng)友

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>)

示例二:

在遇到寫類似的組件的時候需要寫很多很長的代碼,出于簡潔(懶惰使人進(jìn)步)的角度來說,我們應(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> 

然后進(jìn)一步給你的組件加入你想要的樣式需要事件,變得有血有肉

 <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()下是很痛苦的,不利于維護(hù)。

所以通常會

var com1= createElement('p','item1');varcom2= createElement('p','item1');

可以使用return createElement('div',[com1,com2])

這種情況是禁止的return createElement('div',[com1,com1])

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 武乡县| 保德县| 深州市| 德安县| 米易县| 鹤庆县| 泰宁县| 葫芦岛市| 水富县| 肥西县| 工布江达县| 额敏县| 黄石市| 军事| 弋阳县| 巴彦淖尔市| 紫金县| 景东| 株洲市| 新营市| 阿克苏市| 亚东县| 突泉县| 巨鹿县| 阜城县| 沛县| 封开县| 赫章县| 涞源县| 皮山县| 西吉县| 通渭县| 丽江市| 玉屏| 郁南县| 淮安市| 沂水县| 嘉善县| 比如县| 昆山市| 云梦县|