Skip to content

Commit

Permalink
modify: 优化 current 值的更新
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyucoding committed Nov 11, 2018
1 parent 350e668 commit 6238ffb
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions examples/hello-uniapp/components/segmented-control.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<view class="segmented-control" :class="styleType" :style="wrapStyle">
<view v-for="(item, index) in values" class="segmented-control-item" :class="styleType" :key="index" :style="index === current ? activeStyle : itemStyle"
@click="onClick(index)">
<view v-for="(item, index) in values" class="segmented-control-item" :class="styleType" :key="index" :style="index === currentIndex ? activeStyle : itemStyle"
@click="onClick(index)">
{{item}}
</view>
</view>
Expand Down Expand Up @@ -30,6 +30,18 @@
default: 'button'
}
},
data() {
return {
currentIndex: this.current
}
},
watch: {
current(val) {
if (val !== this.currentIndex) {
this.currentIndex = val;
}
}
},
computed: {
wrapStyle() {
let styleString = '';
Expand Down Expand Up @@ -70,8 +82,8 @@
},
methods: {
onClick(index) {
if (this.current !== index) {
this.current = index;
if (this.currentIndex !== index) {
this.currentIndex = index;
this.$emit('clickItem', index);
}
}
Expand All @@ -98,7 +110,7 @@
.segmented-control.text {
border: 0;
border-radius: 0upx;
border-radius: 0upx;
}
Expand All @@ -120,4 +132,4 @@
.segmented-control-item:first-child {
border-left-width: 0;
}
</style>
</style>

0 comments on commit 6238ffb

Please sign in to comment.