我想自己实现一个tabbar
的组件,因此定义了两个组件来组合实现:
1. TabBar
<template>
<div class="tabbar">
<tab-item v-for="item in tabItems"
:key="item.index"
v-bind="item"
@tabItemOnClick="tabItemOnClick"
>
</tab-item>
</div>
</template>
<script>
import TabItem from './tabitem.vue'
export default {
name: 'tab-bar',
components: {TabItem},
props: {
tabItems: {
default: []
}
},
data () {
return {
selectedIndex: 0
}
},
created () {
this.select(this.selectedIndex)
},
methods: {
tabItemOnClick (params) {
this.selectedIndex = params.index
this.select(this.selectedIndex)
this.toast(this.selectedIndex)
},
select (index) {
for (let i = 0; i < this.tabItems.length; i++) {
var tabItem = this.tabItems[i]
if (i === index) {
tabItem.selected = true
} else {
tabItem.selected = false
}
}
}
}
}
</script>
<style>
.tabbar {
background-color: #ffffff;
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 100px;
flex-wrap: nowrap;
flex-direction: row;
}
</style>
2.TabBarItem
<template>
<div class="bar-item" @click="onTabItemClick">
<text class="icon iconfont" :class="[ selected ? 'active' : '' ]">{{ icon }}</text>
<text class="text" :class="[ selected ? 'active' : '' ]">{{ title }}</text>
<div v-if="parseInt(dot) === 0">
<text :class="[ needNum ? 'notice' : 'dot' ]">{{ dot }}</text>
</div>
</div>
</template>
<script>
export default {
name: 'tab-item',
props: {
index: {
type: Number,
required: true
},
icon: {
type: String,
default: ''
},
title: {
type: String,
default: ''
},
dot: {
type: String,
default: '0'
},
needNum: {
type: Boolean,
default: true
},
selected: {
type: Boolean,
default: false
}
},
data () {
return {
}
},
methods: {
setNum (num) {
if (num <= 0) {
this.dot = ''
} else {
this.dot = num + ''
}
},
onTabItemClick () {
var params = {
index: this.index
}
this.$emit('tabItemOnClick', params)
}
},
watch: {
selected: function (val) {
this.toast('selected: ' + this.selected + ', ' + val)
}
}
}
</script>
<style>
.bar-item {
background-color: #afddff;
flex: 1;
}
.text, .icon {
color: #666666;
text-align: center;
}
.iconfont {
font-family: iconfont;
}
.icon {
padding-top: 14px;
font-size: 38px;
}
.text {
font-size: 22px;
padding-top: 2px;
}
.active {
color: #b4282d
}
.notice {
position: absolute;
top: 10px;
right: 30px;
width: 30px;
height: 30px;
border-radius: 100%;
font-size: 26px;
line-height: 30px;;
text-align: center;
color: #ffffff;
background-color: #ff0000;
}
.dot {
position: absolute;
top:15px;
right: 40px;
height: 15px;
width: 15px;
border-radius: 100%;
background-color: #ff0000;
}
</style>
3. 使用
<tab-bar :tabItems="tabs"></tab-bar>
tabs通过computed计算属性赋值:
computed: {
tabs: function () {
return [{
index: 0,
icon: '',
title: '首页',
dot: '0',
needNum: true,
selected: true
},
{
index: 1,
icon: '',
title: '分类',
dot: '1',
needNum: true,
selected: false
},
{
index: 2,
icon: '',
title: '购物车',
dot: '5',
needNum: true,
selected: false
},
{
index: 3,
icon: '',
title: '我',
dot: '1',
needNum: false,
selected: false
}]
}
}
当点击TabBarItem后,点击事件可以传递到父组件并且修改选中tabItem的selected
属性,但修改后tabBarItem组件的style
并没有发生修改,子组件中也watch
不到变更,没有搞懂这是为什么?
为什么要用计算属性啊,你这个计算属性里也没有与data下的任何内容绑定啊。
你直接用 data 试试