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

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

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

在使用jQuery類庫實(shí)現(xiàn)tab功能時(shí),是獲取鼠標(biāo)在mousenter或click時(shí)的index值,然后切換到當(dāng)前的標(biāo)題和內(nèi)容,把其他的標(biāo)題和內(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實(shí)現(xiàn)tab功能時(shí),就不是像jQuery這種直接操作DOM了。我這里總結(jié)了下實(shí)現(xiàn)tab功能的3個(gè)思路,僅供參考。

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

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

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, //當(dāng)前位置
  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就會(huì)切換到對(duì)應(yīng)的標(biāo)簽。

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

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

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

2. 使用currentView

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

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

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

// 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來展示對(duì)應(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屬性可以讓多個(gè)組件可以使用同一個(gè)掛載點(diǎn),并動(dòng)態(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 在組件里就是一個(gè)獨(dú)立的data屬性,能在tab里也使用vue綁定事件了.

3. 使用slot方式等

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

3.1 slot

用slot方式寫一個(gè)子組件:

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"會(huì)替換子組件中name="title"的slot,父組件中slot="content"會(huì)替換子組件中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> 

 這種只需要傳遞一個(gè)list即可。

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

4. 總結(jié)

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

相關(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組件實(shí)現(xiàn)多圖片上傳和壓縮的示例代碼

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

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

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

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

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

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

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

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

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

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

    如何利用Vue3+Element?Plus實(shí)現(xiàn)動(dòng)態(tài)標(biāo)簽頁及右鍵菜單

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

    解決vue內(nèi)存溢出報(bào)錯(cuò)的問題

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

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

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

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

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

最新評(píng)論