Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复u-switch组件v-model绑定非布尔值时报错 #1197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions uview-ui/components/u-switch/u-switch.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<view class="u-switch" :class="[value == true ? 'u-switch--on' : '', disabled ? 'u-switch--disabled' : '']" @tap="onClick"
<view class="u-switch" :class="[checked ? 'u-switch--on' : '', disabled ? 'u-switch--disabled' : '']" @tap="onClick"
:style="[switchStyle]">
<view class="u-switch__node node-class" :style="{
width: $u.addUnit(this.size),
Expand Down Expand Up @@ -55,7 +55,7 @@
},
// 通过v-model双向绑定的值
value: {
type: Boolean,
type: [Number, String, Boolean],
default: false
},
// 是否使手机发生短促震动,目前只在iOS的微信小程序有效(2020-05-06)
Expand Down Expand Up @@ -83,22 +83,26 @@
switchStyle() {
let style = {};
style.fontSize = this.size + 'rpx';
style.backgroundColor = this.value ? this.activeColor : this.inactiveColor;
style.backgroundColor = this.checked ? this.activeColor : this.inactiveColor;
return style;
},
loadingColor() {
return this.value ? this.activeColor : null;
return this.checked ? this.activeColor : null;
},
checked() {
return this.value === this.activeValue;
}
},
methods: {
onClick() {
if (!this.disabled && !this.loading) {
// 使手机产生短促震动,微信小程序有效,APP(HX 2.6.8)和H5无效
if(this.vibrateShort) uni.vibrateShort();
this.$emit('input', !this.value);
const val = this.checked ? this.inactiveValue : this.activeValue;
this.$emit('input', val);
// 放到下一个生命周期,因为双向绑定的value修改父组件状态需要时间,且是异步的
this.$nextTick(() => {
this.$emit('change', this.value ? this.activeValue : this.inactiveValue);
this.$emit('change', val);
})
}
}
Expand Down