欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年01月31日 15:50:03   作者:Choo  
本篇文章主要介紹了Vue中render函數(shù)的使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

render函數(shù)

vue通過(guò) template 來(lái)創(chuàng)建你的 HTML。但是,在特殊情況下,這種寫死的模式無(wú)法滿足需求,必須需要js的編程能力。此時(shí),需要用render來(lái)創(chuàng)建HTML。

什么情況下適合使用render函數(shù)

在一次封裝一套通用按鈕組件的工作中,按鈕有四個(gè)樣式(default success error )。首先,你可能會(huì)想到如下實(shí)現(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>

這樣寫在按鈕樣式少的時(shí)候完全沒有問題,但是試想,如果需要的按鈕樣式有十多個(gè),按鈕內(nèi)的文字根據(jù)實(shí)際情況而定(如success按鈕內(nèi)的文字可能是OK、GOOD等等)。那么template寫死的方式就顯得很無(wú)力了。遇上類似這樣的情況,使用render函數(shù)可以說(shuō)最優(yōu)選擇了。

根據(jù)實(shí)際情況改寫按鈕組件

首先render函數(shù)生成的內(nèi)容相當(dāng)于template的內(nèi)容,故使用render函數(shù)時(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ù)組件化思維,能抽象出來(lái)的東西絕不寫死在邏輯上。這里的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

是的,要記住每個(gè)參數(shù)的類型同用法,按序傳參實(shí)在是太麻煩了。那么其實(shí)可以用jsx來(lái)優(yōu)化這個(gè)繁瑣的過(guò)程。

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

示例二:

在遇到寫類似的組件的時(shí)候需要寫很多很長(zhǎng)的代碼,出于簡(jiǎn)潔(懶惰使人進(jìn)步)的角度來(lái)說(shuō),我們應(yīng)該找到更合適的方法來(lái)實(shí)現(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> 

這時(shí)候Render 函數(shù)就很好的解決了這個(gè)問題,先來(lái)簡(jiǎn)單一點(diǎn)額例子,算是有基本的骨架了

 <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 必須是唯一的。

直接把所有元素寫在一個(gè)createElement()下是很痛苦的,不利于維護(hù)。

所以通常會(huì)

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

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

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解element-ui級(jí)聯(lián)菜單(城市三級(jí)聯(lián)動(dòng)菜單)和回顯問題

    詳解element-ui級(jí)聯(lián)菜單(城市三級(jí)聯(lián)動(dòng)菜單)和回顯問題

    這篇文章主要介紹了詳解element-ui級(jí)聯(lián)菜單(城市三級(jí)聯(lián)動(dòng)菜單)和回顯問題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Vue3實(shí)現(xiàn)自定義Input組件的示例詳解

    Vue3實(shí)現(xiàn)自定義Input組件的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何使用Vue3自定義實(shí)現(xiàn)一個(gè)類似el-input的組件,可以v-model雙向綁定,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • 關(guān)于Vue3中element-plus的el-dialog對(duì)話框無(wú)法顯示的問題及解決方法

    關(guān)于Vue3中element-plus的el-dialog對(duì)話框無(wú)法顯示的問題及解決方法

    最近今天在寫一個(gè)停車場(chǎng)管理系統(tǒng)的項(xiàng)目時(shí),在用vue3寫前端時(shí),在前端模板選擇上,我一時(shí)腦抽,突然決定放棄SpringBoot,選擇了以前幾乎很少用的element-plus,然后果不其然,錯(cuò)誤連連,下面給大家分享dialog對(duì)話框無(wú)法顯示的原因,感興趣的朋友一起看看吧
    2023-10-10
  • vue中使用axios請(qǐng)求post接口發(fā)送兩次

    vue中使用axios請(qǐng)求post接口發(fā)送兩次

    這篇文章主要為大家介紹了vue中使用axios請(qǐng)求post接口,請(qǐng)求會(huì)發(fā)送兩次原因解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 關(guān)于SpringBoot與Vue交互跨域問題解決方案

    關(guān)于SpringBoot與Vue交互跨域問題解決方案

    最近在利用springboot+vue整合開發(fā)一個(gè)前后端分離的個(gè)人博客網(wǎng)站,所以這一篇總結(jié)一下在開發(fā)中遇到的一個(gè)問題,關(guān)于解決在使用vue和springboot在開發(fā)前后端分離的項(xiàng)目時(shí),如何解決跨域問題。在這里分別分享兩種方法,分別在前端vue中解決和在后臺(tái)springboot中解決。
    2021-10-10
  • 在vue中寫jsx的幾種方式

    在vue中寫jsx的幾種方式

    本文主要介紹了在vue中寫jsx的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Vuex之module使用方法及場(chǎng)景說(shuō)明

    Vuex之module使用方法及場(chǎng)景說(shuō)明

    這篇文章主要介紹了Vuex之module使用方法及場(chǎng)景說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue項(xiàng)目中訪問本地json數(shù)據(jù)

    vue項(xiàng)目中訪問本地json數(shù)據(jù)

    這篇文章主要介紹了vue項(xiàng)目中訪問本地json數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Vue3引入SVG圖標(biāo)的流程步驟

    Vue3引入SVG圖標(biāo)的流程步驟

    我們?cè)陂_發(fā) Vue 項(xiàng)目的時(shí)候會(huì)使用一些前端組件庫(kù),例如 Element、Ant Design 等,這些組件庫(kù)雖然方便,但是也有一些缺點(diǎn),比如內(nèi)置的圖標(biāo)太少,例如我們開發(fā)醫(yī)療、財(cái)務(wù)、工程等一些前端項(xiàng)目,內(nèi)置的圖標(biāo)不能滿足我們的需求,所以我們常常在Vue項(xiàng)目中引入SVG圖標(biāo)
    2024-09-09
  • vue中activated的用法

    vue中activated的用法

    這篇文章主要介紹了vue中activated的用法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2021-01-01

最新評(píng)論