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

詳解vue渲染函數(shù)render的使用

 更新時間:2017年12月12日 11:39:10   作者:沒文化不開心  
本篇文章主要介紹了vue渲染函數(shù)render的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1.什么是render函數(shù)?

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

比如如下我想要實現(xiàn)如下html:

<div id="container">
 <h1>
  <a href="#" rel="external nofollow" rel="external nofollow" >
   Hello world!
  </a>
 </h1>
</div>

我們會如下使用:

 <!DOCTYPE html>
<html>
 <head>
  <title>演示Vue</title>
  <style>
   
  </style>
 </head>
 <body>
  <div id="container">
   <tb-heading :level="1">
    <a href="#" rel="external nofollow" rel="external nofollow" >Hello world!</a>
   </tb-heading>
  </div>
 </body>
 <script src="./vue.js"></script>

 <script type="text/x-template" id="templateId">
  <h1 v-if="level === 1">
   <slot></slot>
  </h1>
  <h2 v-else-if="level === 2">
   <slot></slot>
  </h2>
 </script>
 <script>
  Vue.component('tb-heading', {
   template: '#templateId',
   props: {
    level: {
     type: Number,
     required: true
    }
   }
  });
  new Vue({
   el: '#container'
  });
 </script>
</html>

 2.例:

遇到的問題:

在工作中,我創(chuàng)建了一個button組件,又創(chuàng)建了一個button-group組件

button組件較為簡單,就是一個可以輸入type/size/icon等屬性的button

button渲染后結(jié)果

此為渲染后結(jié)果。

然后,創(chuàng)建button-group組件,目標結(jié)果為

目標結(jié)果

此處,不僅要在最外層包裹一層div,還要在每個button組件外層包裹一層p標簽。此處,就需要使用render函數(shù)了。

既然有了render函數(shù),就不再需要template標簽了,vue文件中只需要script標簽(該組件style是全局的)

button-group.vue如下

<script>
 export default {
  name: "XButtonGroup",
  props: {
   compact: { //自定義的button-group屬性,影響其classname
    type: Boolean,
    default: true
   }
  },
  render(createElement) {
   //此處創(chuàng)建element
  },
  computed: {
   groupClass() {
    const className = ["field"];  //通過計算屬性監(jiān)聽compact屬性傳入className
    className.push(this.compact ? "has-addons" : "is-grouped");
    return className;
   }
  }
 };
</script>

接下來就要看render函數(shù)了。

render函數(shù)中的createElement方法有三個參數(shù)。第一個參數(shù)為外層標簽名,第二個為外層標簽的屬性對象,第三個為外層標簽中的內(nèi)容

所以第一步

  render(createElement) {
   return createElement(
    'div', {
     class: this.groupClass
    }, '內(nèi)容',
   )
  }

渲染結(jié)果:

那怎樣在外層div中渲染button組件呢?

render函數(shù)的第三個參數(shù)除了字符串,還可以傳入VNode的數(shù)組。VNode就是vue中的節(jié)點。

此處,我們通過this.$slots.default獲取所有插入到button-group組件內(nèi)默認slot的button節(jié)點

  render(createElement) {
   return createElement(
    'div', {
     class: this.groupClass
    }, this.$slots.default,
   )
  },

渲染結(jié)果:

 

button已經(jīng)正確渲染到了外層div中。但是怎么在每個button外層包裹一層元素呢。createElement會創(chuàng)建新的VNode,而render函數(shù)第三個參數(shù)需要VNode數(shù)組,所以我們需要傳入一個由createElement返回值組成的數(shù)組。

  render(createElement) {
   //遍歷每一個VNode,用createElement函數(shù)在外層包裹class為control的p標簽,組成新的VNode數(shù)組
   const arry = this.$slots.default.map(VNode => {
    return createElement('p', {
     class: 'control'
    }, [VNode])
   })
   return createElement(
    'div', {
     class: this.groupClass
    }, arry,
   )
  },

渲染結(jié)果:

 

并且根據(jù)button-group的compact屬性可以切換不同的class,生成不同的效果

<x-button-group :compact="true">
  <x-button v-for="(item,index) in buttonType" :key="index" :type="item">{{item}}</x-button>
</x-button-group>
<x-button-group :compact="false">
  <x-button v-for="(item,index) in buttonType" :key="index" :type="item">{{item}}</x-button>
</x-button-group>

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

相關(guān)文章

最新評論