首頁 web前端 js教程 Vue中render函數基本用法(詳細教學)

Vue中render函數基本用法(詳細教學)

Jun 08, 2018 pm 04:03 PM
render函數 vue

這篇文章主要介紹了Vue中render函數的使用方法,現在分享給大家,也給大家做個參考。

render函數

vue透過 template 來建立你的 HTML。但是,在特殊情況下,這種寫死的模式無法滿足需求,必須需要js的程式設計能力。此時,需要用render來建立HTML。

什麼情況下適合使用render函數

在一次封裝一套通用按鈕元件的工作中,按鈕有四個樣式(default success error )。首先,你可能會想到如下實作

 <p v-if="type === &#39;success&#39;">success</p>
 <p v-else-if="type === &#39;error&#39;">error</p>
 <p v-else-if="type === &#39;warm&#39;">warm</p>
 <p v-else>default</p>
登入後複製

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

根據實際情況改寫按鈕元件

首先render函數產生的內容相當於template的內容,故使用render函數時,在.vue檔案中需要先把template標籤去掉。只保留邏輯層。

export default {
 render(h) {
  return h(&#39;p&#39;,{
   &#39;class&#39;: {
    btn: true,
    success: this.type === &#39;success&#39;,
    error: this.type === &#39;error&#39;,
    warm: this.type === &#39;warm&#39;,
    default: this.type === &#39;default&#39;
   },
   domProps: {
    innerHTML: this.$slots.default[0].text
   },
   on: {
    click: this.clickHandle
   }
  })
 },
 methods: {
  clickHandle() {
   // dosomething
  }
 },
 props: {
  type: {
   type: String,
   default: &#39;default&#39;
  },
  text: {
   type: String,
   default: &#39;default&#39;
  }
 }
};
登入後複製

根據元件化思維,能抽像出來的東西絕不寫死在邏輯上。這裡的clickHandle函數可以根據按鈕的type型別觸發不同的邏輯,就不多敘述了。

然後在父元件呼叫

<btn
 v-for="(btn, index) in testData"
 :type="btn.type"
 :text="btn.text"
 :key="index">{{btn.text}}
</btn>
登入後複製

使用jsx

是的,要記住每個參數的類型同用法,按序傳參實在是太麻煩了。那其實可以用jsx來優化這個繁瑣的流程。

return (
 <p
  class={{
   btn: true,
   success: this.type === &#39;success&#39;,
   error: this.type === &#39;error&#39;,
   warm: this.type === &#39;warm&#39;,
   default: this.type === &#39;default&#39;
  }}
  onClick={this.clickHandle}>
  {this.$slots.default[0].text}
 </p>
)
登入後複製

範例二:

在遇到寫類似的元件的時候需要寫很多很長的程式碼,出於簡潔(懶惰使人進步)的角度來說,我們應該找到更合適的方法來實現該效果。

 <body> 
    <p id="app"> 
      <mycomment :level="2"> 
        这是h2元素 
      </mycomment> 
    </p> 
  </body> 
  <script type="text/x-template" id="is"> 
 <p> 
  <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> 
 </p> 
</script> 
  <script> 
    Vue.component(&#39;mycomment&#39;,{ 
      template:&#39;#is&#39;, 
      props:{ 
        level:{ 
          type:Number, 
          required:true, 
        } 
      } 
    }) 
    var app =new Vue({ 
      el:&#39;#app&#39;, 
    }) 
   </script>
登入後複製

這時候Render 函數就很好的解決了這個問題,先來簡單一點額例子,算是有基本的骨架了

 <body> 
  <p id="app"> 
    <render-teample :level="4"> 
      render function 
 
    </render-teample> 
  </p> 
 
</body> 
<script> 
Vue.component(&#39;render-teample&#39;,{ 
  render:function(createElement){ 
    return createElement( 
      &#39;h&#39;+this.level, 
      this.$slots.default 
      ) 
  }, 
   props: { 
  level: { 
   type: Number, 
   required: true 
  } 
} 
  var app=new Vue({ 
    el:"#app", 
 
  }); 
 </script>
登入後複製

然後進一步給你的元件加入你想要的樣式需要事件,變得有血有肉

 <body> 
    <p id="app"> 
      <render-teample :level="4" > 
 
        <p class="jah" slot="myslot">render function</p> 
      </render-teample> 
    </p> 
 
  </body> 
  <script> 
  Vue.component(&#39;render-teample&#39;,{ 
    render:function(createElement){ 
      return createElement( 
        &#39;h&#39;+this.level, 
        { 
          &#39;class&#39;:{ 
            show:true, 
            hide:false, 
          }, 
          style:{ 
            width:&#39;200px&#39;, 
            height:&#39;400px&#39;, 
            background:&#39;red&#39;, 
          }, 
          attrs:{ 
            name:&#39;h-ex&#39;, 
            id:&#39;h-id&#39; 
          }, 
          props:{ 
            myprops:true, 
          }, 
           on: { 
          click: function(event){ 
            alert(this.num) 
          } 
        }, 
          nativeOn:{ 
            click:function(event) { 
 
              alert(1111) 
            } 
          } 
 
        }, 
        [ 
          this.$slots.myslot, 
          createElement(&#39;p&#39;,{ 
             domProps:{ 
            innerHTML:&#39;holle render&#39; 
          } 
          }) 
        ] 
 
        ) 
    }, 
     props: { 
    level: { 
     type: Number, 
     required: true 
    } 
  } 
});  
    var app=new Vue({ 
      el:"#app", 
      data:{ 
        num:110 
      } 
    }); 
  </script>
登入後複製

注意:約束元件中VNodes 必須是唯一的。

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

所以通常會

var com1= createElement(&#39;p&#39;,&#39;item1&#39;);var
com2= createElement(&#39;p&#39;,&#39;item1&#39;);
登入後複製

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

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

上面是我整理給大家的,希望今後會對大家有幫助。

相關文章:

在Node.js中如何實作mysql事務自動回收連線

在JS陣列中如何刪除某個元素

詳細介紹js中有關promise的知識點

#

以上是Vue中render函數基本用法(詳細教學)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
vue中怎麼用bootstrap vue中怎麼用bootstrap Apr 07, 2025 pm 11:33 PM

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

vue怎麼給按鈕添加函數 vue怎麼給按鈕添加函數 Apr 08, 2025 am 08:51 AM

可以通過以下步驟為 Vue 按鈕添加函數:將 HTML 模板中的按鈕綁定到一個方法。在 Vue 實例中定義該方法並編寫函數邏輯。

vue中的watch怎麼用 vue中的watch怎麼用 Apr 07, 2025 pm 11:36 PM

Vue.js 中的 watch 選項允許開發者監聽特定數據的變化。當數據發生變化時,watch 會觸發一個回調函數,用於執行更新視圖或其他任務。其配置選項包括 immediate,用於指定是否立即執行回調,以及 deep,用於指定是否遞歸監聽對像或數組的更改。

vue返回上一頁的方法 vue返回上一頁的方法 Apr 07, 2025 pm 11:30 PM

Vue.js 返回上一頁有四種方法:$router.go(-1)$router.back()使用 &lt;router-link to=&quot;/&quot;&gt; 組件window.history.back(),方法選擇取決於場景。

vue多頁面開發是啥意思 vue多頁面開發是啥意思 Apr 07, 2025 pm 11:57 PM

Vue 多頁面開發是一種使用 Vue.js 框架構建應用程序的方法,其中應用程序被劃分為獨立的頁面:代碼維護性:將應用程序拆分為多個頁面可以使代碼更易於管理和維護。模塊化:每個頁面都可以作為獨立的模塊,便於重用和替換。路由簡單:頁面之間的導航可以通過簡單的路由配置來管理。 SEO 優化:每個頁面都有自己的 URL,這有助於搜索引擎優化。

React與Vue:Netflix使用哪個框架? React與Vue:Netflix使用哪個框架? Apr 14, 2025 am 12:19 AM

NetflixusesAcustomFrameworkcalled“ Gibbon” BuiltonReact,notReactorVuedIrectly.1)TeamSperience:selectBasedonFamiliarity.2)ProjectComplexity:vueforsimplerprojects:reactforforforproproject,reactforforforcompleplexones.3)cocatizationneedneeds:reactoffipicatizationneedneedneedneedneedneeds:reactoffersizationneedneedneedneedneeds:reactoffersizatization needefersmoreflexibleise.4)

vue.js怎麼引用js文件 vue.js怎麼引用js文件 Apr 07, 2025 pm 11:27 PM

在 Vue.js 中引用 JS 文件的方法有三種:直接使用 &lt;script&gt; 標籤指定路徑;利用 mounted() 生命週期鉤子動態導入;通過 Vuex 狀態管理庫進行導入。

vue遍歷怎麼用 vue遍歷怎麼用 Apr 07, 2025 pm 11:48 PM

Vue.js 遍歷數組和對像有三種常見方法:v-for 指令用於遍歷每個元素並渲染模板;v-bind 指令可與 v-for 一起使用,為每個元素動態設置屬性值;.map 方法可將數組元素轉換為新數組。

See all articles