From d355989fcfb560dcbb2380eadf0a9acbc1037f87 Mon Sep 17 00:00:00 2001 From: aisen Date: Wed, 28 Jan 2026 10:23:14 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E4=BF=AE=E5=A4=8Dswitch=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E4=BA=A4=E4=BA=92=E6=A8=A1=E5=BC=8F=EF=BC=8C=E4=B8=A5?= =?UTF-8?q?=E6=A0=BC=E9=81=B5=E5=AE=88mvvm=E6=80=9D=E6=83=B3=EF=BC=8C?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8v-model=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=80=BC=E5=90=8E=EF=BC=8C=E5=88=87=E6=8D=A2=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=94=B9=E7=BB=91=E5=AE=9A=E7=9A=84=E5=80=BC?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E5=86=8D=E9=9C=80=E8=A6=81@change=E9=87=8C?= =?UTF-8?q?=E9=9D=A2=E5=8E=BB=E8=8E=B7=E5=8F=96=EF=BC=8C=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E4=BA=86=E5=BC=80=E5=8F=91=E5=B7=A5=E4=BD=9C=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tuniao-ui/components/tn-switch/tn-switch.vue | 29 ++++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/tuniao-ui/components/tn-switch/tn-switch.vue b/tuniao-ui/components/tn-switch/tn-switch.vue index ba9c419..7280813 100644 --- a/tuniao-ui/components/tn-switch/tn-switch.vue +++ b/tuniao-ui/components/tn-switch/tn-switch.vue @@ -2,7 +2,7 @@ @@ -31,7 +31,7 @@ class="tn-switch__icon tn-switch__icon--right" :class="[ `tn-icon-${rightIcon}`, - !value ? 'tn-switch__icon--show' : '' + !switchState ? 'tn-switch__icon--show' : '' ]" :style="[iconStyle]"> @@ -42,7 +42,7 @@ name: 'tn-switch', props: { value: { - type: Boolean, + type: [Number, String, Boolean], default: false }, // 按钮的样式 @@ -106,7 +106,7 @@ switchStyle() { let style = {} style.fontSize = this.$tn.string.getLengthUnitValue(this.size) - style.backgroundColor = this.value ? + style.backgroundColor = this.switchState ? this.activeColor ? this.activeColor : '#01BEFF' : this.inactiveColor ? this.inactiveColor : '#AAAAAA' return style @@ -124,22 +124,33 @@ return style }, loadingColor() { - return this.value ? this.activeColor : '' + return this.switchState ? this.activeColor : '' + } + }, + watch:{ + value:{ + handler(newVal,oldVal){ + this.switchState = (this.value == this.activeValue); + } } }, data() { return { - + switchState:false } }, + mounted() { + this.switchState = (this.value == this.activeValue); + }, methods: { click() { + this.switchState = !this.switchState; if (!this.disabled && !this.loading) { if (this.vibrateShort) uni.vibrateShort() - this.$emit('input', !this.value) + this.$emit('input', this.switchState ? this.activeValue : this.inactiveValue) // 放到下一个生命周期,因为双向绑定的value修改父组件状态需要时间,且是异步的 this.$nextTick(() => { - this.$emit('change', this.value ? this.activeValue : this.inactiveValue); + this.$emit('change', this.switchState ? this.activeValue : this.inactiveValue); }) } }