组件源码:
https://github.com/AntJavascript/widgetUI/tree/master/Tab
组件结构代码:
<template>
<div class="wt-tab">
<div class="tab">
<ul :class="type">
<li v-for="(item, index) in dataSource" :key="index" @click="handle(item, index)" :class="{active:active == index}">
{{item.text}}
</li>
</ul>
</div>
</div>
</template>
代码分析:
props参数:
props: {
dataSource: { // 数据来源
type: Array,
default: () => {
return [];
}
},
currIndex: { // 当前处于第几个选项
type: Number | String,
default: () => {
return 0;
}
},
type: { // 组件类型,(可选)(可选值:"default", "full")
type: String,
default: () => {
return 'default';
}
}
}
data参数:
data () {
return {
active: '' // 当前处于第几个选项
};
}
created生命周期:
created () {
this.active = this._props.currIndex; // 获取父组件的参数值
}
methods函数:
methods: {
handle (item, index) {
this.active = index; // 设置当前选项
this.$emit('handle', item, index); // 传值父组件
}
}
css代码:
.wt-tab {
width: 100%;
.tab {
background: #fff;
// border-bottom: 1px solid #ebebeb;
&::after {
transform: scaleY(.5);
height: 1px;
content: '';
border-bottom: 1px solid #ccc;
display: block;
}
ul {
display: flex;
font-size: 0.7rem;
&.default {
justify-content: space-around;
}
&.full {
justify-content: space-between;
li {
flex: 1;
text-align: center;
}
}
li {
height: 2rem;
line-height: 2rem;
&.active {
border-bottom: 2px solid #1BB5F1;
margin-bottom: -1px;
color: #1BB5F1;
}
}
}
}
}
组件源码:
https://github.com/AntJavascript/widgetUI/tree/master/Tab
网友评论