国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解使用vue實現tab 切換操作

2019-11-19 16:12:29
字體:
來源:轉載
供稿:網友

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

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

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

1. 切換content或者直接切換內容

這種思路下,我們首先把結構搭建起來,然后用一個變量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就會切換到對應的標簽。

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

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

每次selected變換時,bd的內容都會發生變化。

2. 使用currentView

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

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

每個組件里展示的是一個tab里的內容,先注冊3個組件:

// tab0Vue.component('item0',{ template : '<div>1111111content</div>'});// tab1Vue.component('item1',{ template : '<div>222222content</div>'})// tab2Vue.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來展示對應組件的內容,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屬性可以讓多個組件可以使用同一個掛載點,并動態切換:

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方式進行內容分發或者一個獨立的組件,可以讓我們把代碼整合到一塊,對外提供一個數據接口,只要按照既定的格式填寫數據即可。

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結構與上面之前的代碼一樣。

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中可以自定義更多的內容,而下面的方法使用起來更加簡單,只是自定義的東西比較少。

4. 總結

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄平县| 和平区| 衡山县| 平塘县| 宜宾市| 集安市| 亳州市| 克什克腾旗| 荥阳市| 抚松县| 涞水县| 镶黄旗| 沙洋县| 中江县| 大悟县| 开原市| 建始县| 竹山县| 浙江省| 敖汉旗| 崇信县| 东乡县| 大埔区| 上高县| 武宁县| 台山市| 龙南县| 永嘉县| 丰县| 尤溪县| 石柱| 丘北县| 长寿区| 新乡县| 神农架林区| 吴江市| 铁力市| 井冈山市| 理塘县| 扬中市| 淮安市|