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

詳解使用vue實現(xiàn)tab 切換操作

 更新時間:2017年07月03日 08:22:27   作者:Published  
這篇文章主要介紹了詳解使用vue實現(xiàn)tab操作,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在使用jQuery類庫實現(xiàn)tab功能時,是獲取鼠標在mousenter或click時的index值,然后切換到當前的標題和內(nèi)容,把其他的標題和內(nèi)容的狀態(tài)去掉:

$('.tab .title').find('.item')
 .removeClass('current').eq(index).addClass('current'); // 為index位置的title添加current
$('.tab .content').find('.item')
 .hide().eq(index).show(); // 顯示index位置的內(nèi)容

那么在使用vue實現(xiàn)tab功能時,就不是像jQuery這種直接操作DOM了。我這里總結(jié)了下實現(xiàn)tab功能的3個思路,僅供參考。

1. 切換content或者直接切換內(nèi)容

這種思路下,我們首先把結(jié)構(gòu)搭建起來,然后用一個變量selected表示tab當前展示的位置,給li標簽添加mouseenter或click事件,將當前的index傳遞進去:

html代碼:

<div class="hd">
 <ul class="clearfix">
  <li v-for="(item, index) of list" :class="{active:selected==index}" @mouseenter="change(index)">{{item.title}}</li>
 </ul>
</div>
<div v-for="(item, index) of list" :class="{active:selected==index, item:true}" v-html="item.content"></div>

js代碼:

var app = new Vue({
 el: '#app',
 data: {
  selected: 0, //當前位置
  list: [
   {
    title: '11111',
    content: '11111content'
   },
   {
    title: '22222',
    content: '222222content'
   },
   {
    title: '33333',
    content: `<div>
        <span style="color:#f00">hello world</span>
        <p><input type="text" v-model="message"></p>
        <p>{{message}}</p>
       </div>`
   }
  ]
 },
 methods: {
  change(index) {
   this.selected = index;
  }
 }
})

綁定的change(index)事件,每次都將index給了selected,然后tab就會切換到對應(yīng)的標簽。

上面的代碼里,我們是通過切換div的顯示與隱藏來進行執(zhí)行的。tab中的content里如果只有純html內(nèi)容,我們可以直接把list[selected].content展示到.bd中:

<div class='bd' v-html="list[selected].content"></div>

每次selected變換時,bd的內(nèi)容都會發(fā)生變化。

2. 使用currentView

在上面的實現(xiàn)方式中,第3個tab里有個輸入框與p標簽雙向綁定,但是沒有效果,因為vue是把list中的內(nèi)容作為html元素填充到頁面中的,message并沒有作為vue的屬性綁定給input。那么使用組建和currentView就能彌補這個缺陷。

無論使用全局注冊還是局部注冊的組件,思路都是一樣的,我們暫時使用全局注冊的組件來實現(xiàn)。

每個組件里展示的是一個tab里的內(nèi)容,先注冊3個組件:

// tab0
Vue.component('item0',{
 template : '<div>1111111content</div>'
});
// tab1
Vue.component('item1',{
 template : '<div>222222content</div>'
})
// tab2
Vue.component('item2',{
 data(){
  return{
   message : ''
  }
 },
 template : `<div>
     <span style="color:#f00">hello world</span>
     <p><input type="text" v-model="message"></p>
     <p>{{message}}</p>
    </div>`
})

然后在html中使用component來展示對應(yīng)組件的內(nèi)容,title的展示方式不變:

<div class="hd">
 <ul class="clearfix">
  <li v-for="(item, index) of list" :class="{active:selected==index}" @mouseenter="change(index)">{{item.title}}</li>
 </ul>
</div>
<component :is="currentView"></component>

currentView屬性可以讓多個組件可以使用同一個掛載點,并動態(tài)切換:

var app = new Vue({
 el: '#app',
 data: {
  selected: 0,
  currentView : 'item0',
  list: [
   {
    title: '11111'
   },
   {
    title: '22222'
   },
   {
    title: '33333'
   }
  ]
 },
 methods: {
  change(index) {
   this.selected = index;
   this.currentView = 'item'+index; // 切換currentView
  }
 }
})

這樣 message 在組件里就是一個獨立的data屬性,能在tab里也使用vue綁定事件了.

3. 使用slot方式等

使用slot方式進行內(nèi)容分發(fā)或者一個獨立的組件,可以讓我們把代碼整合到一塊,對外提供一個數(shù)據(jù)接口,只要按照既定的格式填寫數(shù)據(jù)即可。

3.1 slot

用slot方式寫一個子組件:

Vue.component('my-slot-tab', {
 props : ['list', 'selected'],
 template : `<div class="tab">
     <div class="hd">
      <ul class="clearfix">
       <slot name="title" v-for="(item, index) in list" :index="index" :text="item.title"> </slot>
      </ul>
     </div>
     <div class="bd">
      <slot name="content" :content="list[selected].content"></slot>
     </div>
    </div>`
});

父組件模板:

<my-slot-tab :list="list" :selected="selected">
 <template slot="title" scope="props">
  <li :class="{active:selected==props.index, item:true}" @mouseenter="change(props.index)">{{ props.text }}</li>
 </template>
 <template slot="content" scope="props">
  <div v-html="props.content"></div>
 </template>
</my-slot-tab>

父組件中slot="title"會替換子組件中name="title"的slot,父組件中slot="content"會替換子組件中name="content"的slot.最終渲染出來的tab結(jié)構(gòu)與上面之前的代碼一樣。

3.2 其他組件方式

還有一種方式就是把所有的模板都寫到組件中。

子組件:

Vue.component('my-tab', {
 props : ['list'],
 template : `<div class="tab">
     <div class="hd">
      <ul class="clearfix">
       <li v-for="(item, index) in list" :class="{active:selected==index, item:true}" @mouseenter="change(index)">{{item.title}}</li>
      </ul>
     </div>
     <div class="bd">
      <div v-for="(item, index) of list" :class="{active:selected==index, item:true}" v-html="item.content"></div>
     </div>
    </div>`,
 data(){
  return{
   selected:0
  }
 },
 methods : {
  change(index){
   this.selected = index;
  }
 }
});

父組件:

<my-tab :list="list"></my-tab> 

 這種只需要傳遞一個list即可。

對比這兩種方法,slot中可以自定義更多的內(nèi)容,而下面的方法使用起來更加簡單,只是自定義的東西比較少。

4. 總結(jié)

上面講解了幾種實現(xiàn)tab功能的方式,沒有說哪種方式最好,選擇最適合自己項目需求的方式就是最好的。文中有哪有錯誤或不足,歡迎批評指正。也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue安裝與使用

    Vue安裝與使用

    Vue是一套用于構(gòu)建前后端分離的框架。剛開始是由國內(nèi)優(yōu)秀選手尤雨溪開發(fā)出來的,目前是全球“最”流行的前端框架。使用vue開發(fā)網(wǎng)頁很簡單,并且技術(shù)生態(tài)環(huán)境完善,社區(qū)活躍,是前后端找工作必備技能!下面來看看其得安裝及使用方法吧
    2021-10-10
  • 用element的upload組件實現(xiàn)多圖片上傳和壓縮的示例代碼

    用element的upload組件實現(xiàn)多圖片上傳和壓縮的示例代碼

    這篇文章主要介紹了用element的upload組件實現(xiàn)多圖片上傳和壓縮的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-02-02
  • vue+導(dǎo)航錨點聯(lián)動-滾動監(jiān)聽和點擊平滑滾動跳轉(zhuǎn)實例

    vue+導(dǎo)航錨點聯(lián)動-滾動監(jiān)聽和點擊平滑滾動跳轉(zhuǎn)實例

    今天小編就為大家分享一篇vue+導(dǎo)航錨點聯(lián)動-滾動監(jiān)聽和點擊平滑滾動跳轉(zhuǎn)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 基于vue實現(xiàn)分頁/翻頁組件paginator示例

    基于vue實現(xiàn)分頁/翻頁組件paginator示例

    本篇文章主要介紹了基于vue實現(xiàn)分頁/翻頁組件paginator示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • vue.js+Element實現(xiàn)表格里的增刪改查

    vue.js+Element實現(xiàn)表格里的增刪改查

    本篇文章主要介紹了vue.js+Element實現(xiàn)增刪改查,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01
  • Vue頁面刷新記住頁面狀態(tài)的實現(xiàn)

    Vue頁面刷新記住頁面狀態(tài)的實現(xiàn)

    這篇文章主要介紹了Vue頁面刷新記住頁面狀態(tài)的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-12-12
  • 如何利用Vue3+Element?Plus實現(xiàn)動態(tài)標簽頁及右鍵菜單

    如何利用Vue3+Element?Plus實現(xiàn)動態(tài)標簽頁及右鍵菜單

    標簽頁一般配合菜單實現(xiàn),當你點擊一級菜單或者二級菜單時,可以增加對應(yīng)的標簽頁,當你點擊對應(yīng)的標簽頁,可以觸發(fā)對應(yīng)的一級菜單或者二級菜單,下面這篇文章主要給大家介紹了關(guān)于如何利用Vue3+Element?Plus實現(xiàn)動態(tài)標簽頁及右鍵菜單的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • 解決vue內(nèi)存溢出報錯的問題

    解決vue內(nèi)存溢出報錯的問題

    這篇文章主要介紹了解決vue內(nèi)存溢出報錯的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue.js原理分析之observer模塊詳解

    Vue.js原理分析之observer模塊詳解

    這篇文章主要介紹了Vue.js中observer模塊的相關(guān)資料,文中通過原理分析介紹還是相對的詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-02-02
  • vue.js el-tooltip根據(jù)文字長度控制是否提示toolTip問題

    vue.js el-tooltip根據(jù)文字長度控制是否提示toolTip問題

    這篇文章主要介紹了vue.js el-tooltip根據(jù)文字長度控制是否提示toolTip問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02

最新評論