Vue.js標(biāo)簽頁(yè)組件使用方法詳解
本文實(shí)例為大家分享了Vue.js標(biāo)簽頁(yè)組件使用的具體代碼,供大家參考,具體內(nèi)容如下
效果

入口頁(yè) index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>標(biāo)簽頁(yè)組件</title>
<link rel="stylesheet" type="text/css" href="style.css" >
</head>
<body>
<div id="app" v-cloak>
<tabs v-model="activeKey">
<pane label="標(biāo)簽一" name="1">
標(biāo)簽一的內(nèi)容
</pane>
<pane label="標(biāo)簽二" name="2">
標(biāo)簽二的內(nèi)容
</pane>
<pane label="標(biāo)簽三" name="3">
標(biāo)簽三的內(nèi)容
</pane>
</tabs>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="pane.js"></script>
<script src="tabs.js"></script>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
activeKey: '1'
}
});
</script>
</body>
</html>
樣式表 style.css
[v-clock]{
display: none;
}
.tabs{
font-size: 14px;
color: #657180
}
.tabs-bar:after{
content: '';
display: block;
width: 100%;
height: 1px;
background: #d7dde4;
margin-top: -1px;
}
.tabs-tab{
display: inline-block;
padding: 4px 16px;
margin-right: 6px;
background: #fff;
border: 1px solid #d7dde4;
cursor: pointer;
position: relative;
}
.tabs-tab-active{
color: #ee99ff;
border-top: 1px solid #3399ff;
border-bottom: 1px solid #fff;
}
.tabs-tab-active:befor{
content: '';
display: block;
height: 1px;
background: #3399ff;
position: absolute;
top: 0;
left: 0;
right: 0;
}
.tabs-content{
padding: 8px 0;
}
標(biāo)簽頁(yè)外層的組件tabs tabs.js
Vue.component('tabs',{
template: '\
<div class="tabs">\
<div class="tabs-bar">\
<!--標(biāo)簽頁(yè)標(biāo)題,這里要用v-for-->\
<div \
:class="tabCls(item)"\
v-for="(item, index) in navList"\
@click="handleChange(index)">\
{{item.label}}\
</div>\
</div>\
<div class="tabs-content">\
<!--這里的slot就是嵌套的pane-->\
<slot></slot>\
</div>\
</div>',
props: {
value: {
type: [String, Number]
}
},
data: function () {
return {
//用于渲染tabs的標(biāo)題
currentValue: this.value,
navList: []
}
},
methods: {
tabCls(item){
return [
'tabs-tab',
{
'tabs-tab-active': item.name === this.currentValue
}
]
},
getTabs(){
//通過(guò)遍歷子組件,得到所有的pane組件
return this.$children.filter(function (item) {
return item.$options.name === 'pane';
});
},
updateNav(){
this.navList = [];
//設(shè)置對(duì)this的引用,在function回調(diào)里,this的指向的并不是Vue實(shí)例
var _this = this;
this.getTabs().forEach((pane, index) => {
_this.navList.push({
label: pane.label,
name: pane.name || index
});
//如果沒有給pane設(shè)置name,默認(rèn)設(shè)置它的索引
if(!pane.name)
pane.name = index;
//設(shè)置當(dāng)前選中的tab的索引
if(index === 0){
if(!_this.currentValue){
_this.currentValue = pane.name || index;
}
}
});
this.updateStatus();
},
updateStatus(){
var tabs = this.getTabs();
var _this = this;
//顯示當(dāng)前選中的tab對(duì)應(yīng)的pane組件,隱藏沒有選中的
tabs.forEach(tab => {
return tab.show = tab.name === _this.currentValue;
});
},
handleChange: function (index) {
var nav = this.navList[index];
var name = nav.name;
this.currentValue = name;
this.$emit('input', name);
this.$emit('on-click', name);
}
},
watch: {
value: val => {
this.currentValue = val;
},
currentValue: function () {
this.updateStatus();
}
}
});
標(biāo)簽頁(yè)嵌套的組件pane pane.js
Vue.component('pane',{
name: 'pane',
template: '\
<div class="pane" v-show="show">\
<slot></slot>\
</div>',
data: function () {
return {
show: true
}
},
props: {
name: String
},
label: {
type: String,
default: ''
},
methods: {
updateNav: function () {
this.$parent.updateNav();
}
},
watch: {
label: function () {
this.updateNav();
}
},
mounted: function () {
this.updateNav();
}
});
更多教程點(diǎn)擊《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 如何在Vue.js中實(shí)現(xiàn)標(biāo)簽頁(yè)組件詳解
- Vue.js每天必學(xué)之組件與組件間的通信
- Vue.js 遞歸組件實(shí)現(xiàn)樹形菜單(實(shí)例分享)
- Vue.js路由組件vue-router使用方法詳解
- Vue.js組件tree實(shí)現(xiàn)無(wú)限級(jí)樹形菜單
- vue.js表格組件開發(fā)的實(shí)例詳解
- Vue.js組件tabs實(shí)現(xiàn)選項(xiàng)卡切換效果
- Vue.js中兄弟組件之間互相傳值實(shí)例
- 詳解vue.js2.0父組件點(diǎn)擊觸發(fā)子組件方法
- 關(guān)于vue.js彈窗組件的知識(shí)點(diǎn)總結(jié)
- 基于vue.js輪播組件vue-awesome-swiper實(shí)現(xiàn)輪播圖
相關(guān)文章
vue+elementui+vuex+sessionStorage實(shí)現(xiàn)歷史標(biāo)簽菜單的示例代碼
本文主要介紹了vue+elementui+vuex+sessionStorage實(shí)現(xiàn)歷史標(biāo)簽菜單的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
vue+threejs寫物體動(dòng)畫之物體縮放動(dòng)畫效果
最近在vue中安裝Three.js,無(wú)聊順便研究一些關(guān)于3D圖形化庫(kù),下面這篇文章主要給大家介紹了關(guān)于vue+threejs寫物體動(dòng)畫之物體縮放動(dòng)畫效果的相關(guān)資料,需要的朋友可以參考下2022-10-10
關(guān)于VUE的編譯作用域及slot作用域插槽問(wèn)題
這篇文章主要介紹了VUE 的編譯作用域及slot作用域插槽問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
vant(ZanUi)結(jié)合async-validator實(shí)現(xiàn)表單驗(yàn)證的方法
這篇文章主要介紹了vant(ZanUi)結(jié)合async-validator實(shí)現(xiàn)表單驗(yàn)證的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12

