vue+自定义组件实现tab面板形式

291 阅读1分钟

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}

.list {
margin: 10px;
display: flex;
}

.list li {
color: burlywood;
border: 1px solid burlywood;
margin: 0 10px;
padding: 5px 10px;
cursor: pointer;
}

.list li.active {
color: chocolate;
border: 1px solid chocolate;
}

input {
width: 50px;
text-align: center;
height: 20px;
}

button {
width: 20px;
height: 25px;
font-size: 20px;
text-align: center;
}
[v-cloak]{
display: none;
}
</style>
</head>

<body>
<div id="app" v-cloak>
{{index1}}---
{{index2}}
<my-menu :list="list" label='类型' @change="index1=$event"></my-menu>
<my-menu :list="list1" label='口味' @change="index2=$event"></my-menu>
</div>
<script>
Vue.component('my-menu',{
props:{
activeIndex:{
type:Number,
default:0
} ,
list:{
type:Array,
required:true
},
label:{
type:String,
required:true
},

},
data() {
return {
index:this.activeIndex
}
},
watch: {
index(val){
this.$emit("change",val)
}
},
template:`
<ul class="list">
{{label}}:
<li @click="index=i" v-for="(item, i) in list" :class='{active:i===index}'>
{{item.name}}
</li>
</ul>
`
})
let vm = new Vue({
el: "#app",
data() {
return {
index1:0,
index2:0,
list:[
{
id:1,name:"巧克力"
},
{
id:2,name:"蛋糕"
},
{
id:3,name:"冰淇淋"
},
],
list1:[
{
id:1,name:"抹茶"
},
{
id:2,name:"原味"
},
{
id:3,name:"玫瑰"
},
]
}
},
})

</script>
</body>

</html>