vueRender中slots的使用的實(shí)例代碼

本文介紹了vue Render中slots的使用的實(shí)例代碼,有需要了解vue Render中slots用法的朋友可參考。希望此文章對(duì)各位有所幫助。

成都創(chuàng)新互聯(lián)專注于廣德網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供廣德營銷型網(wǎng)站建設(shè),廣德網(wǎng)站制作、廣德網(wǎng)頁設(shè)計(jì)、廣德網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造廣德網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供廣德網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

render 中 slot 的一般默認(rèn)使用方式如下:

this.$slots.default 對(duì)用 template的<slot>的使用沒有name 。

想使用多個(gè)slot 的話。需要對(duì)slot命名唯一。使用this.$slots.name 在render中添加多個(gè)slot。

<body> 
  <div class="" id="app"> 
  <myslot> 
    <div>this is slot</div> 
  </myslot> 
 
 
  </div> 
  <script> 
  Vue.component('myslot',{ 
    render:function(createElement){ 
       var he=createElement('div',{domProps:{innerHTML:'this child div'}}); 
      return createElement('div',[he,this.$slots.default]) 
      } 
  }); 
  var app=new Vue({ 
    el:'#app' 
  }) 
  </script> 
  </body>  

多個(gè)slot的使用

<body> 
  <div class="" id="app"> 
  <myslot> 
    <div slot="name1">this is slot</div> 
    <div slot="name2">The position is slot2 </div> 
  </myslot> 
 
 
  </div> 
  <script> 
  Vue.component('myslot',{ 
    render:function(createElement){ 
       var he=createElement('div',{domProps:{innerHTML:'this child div'}}); 
      return createElement('div',[he,this.$slots.name2,this.$slots.name1]) 
      } 
  }); 
  var app=new Vue({ 
    el:'#app' 
  }) 
  </script> 
  </body> 

在vue2.1.0新添加了scope(雖然感覺有點(diǎn)怪,但是用習(xí)慣了,還蠻好用的)。同樣給出一般使用和多個(gè)使用示例,

<body> 
  <div class="" id="app"> 
  <myslot> 
    <template scope="props"> 
      <div>{{props.text}}</div> 
    </template> 
 
  </myslot> 
 
 
  </div> 
  <script> 
  Vue.component('myslot',{ 
    render:function(createElement){ 
       var he=createElement('div',{domProps:{innerHTML:'this child div'}}); 
      return createElement('div',[he,this.$scopedSlots.default({ 
        text:'hello scope' 
      })]) 
      } 
  }); 
  var app=new Vue({ 
    el:'#app' 
  }) 
  </script> 
  </body> 

多個(gè)$scopedSlot的使用

<body> 
  <div class="" id="app"> 
  <myslot> 
    <template slot="name2" scope="props"> 
      <div>{{props.text}}</div> 
    </template> 
    <template slot="name1" scope="props"> 
      <span>{{props.text}}</span> 
    </template> 
 
  </myslot> 
 
 
  </div> 
  <script> 
  Vue.component('myslot',{ 
    render:function(createElement){ 
       var he=createElement('div',{domProps:{innerHTML:'this child div'}}); 
      return createElement('div', 
        [he, 
        this.$scopedSlots.name1({ 
        text:'hello scope' 
      }), 
        this.$scopedSlots.name2({ 
        text:'$scopedSlots using' 
      })]) 
      } 
  }); 
  var app=new Vue({ 
    el:'#app' 
  }) 
  </script> 
  </body> 

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

文章標(biāo)題:vueRender中slots的使用的實(shí)例代碼
文章分享:http://muchs.cn/article22/ijdocc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、網(wǎng)站制作企業(yè)網(wǎng)站制作、響應(yīng)式網(wǎng)站、建站公司、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站