You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.4 KiB
Vue

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<template>
<el-switch
v-model="isEnabled"
:loading="loading"
active-text=""
inactive-text=""
active-value="Y"
inactive-value="N"
:before-change="() => toggleSwitch('测试参数')"
/>
</template>
<script setup>
import { ref, onMounted } from 'vue';
/**
* :before-change="toggleSwitch" 如果不带参数可以使用这种写法不会导致getInitData设置数据之后触发
* :before-change="toggleSwitch(isEnabled)" 如果带参数使用这种写法会导致getInitData设置数据之后触发
* :before-change="() => toggleSwitch(isEnabled)" 无聊是否带参数都可以使用该写法都不会导致getInitData设置数据之后触发
*
* 原因是因为el-switch希望得到的默认值我们自定义为Y或者N但是我们初始化的时候又设置值为空从空切换到Y或者N会触发before-change事件
*/
const isEnabled = ref('');
const loading = ref(false);
const toggleSwitch = (params) => {
console.log('执行一次', params);
loading.value = true;
return new Promise((resolve) => {
setTimeout(() => {
isEnabled.value = isEnabled.value === 'Y' ? 'N' : 'Y';
loading.value = false;
resolve();
}, 3000);
});
};
const getInitData = () => {
setTimeout(() => {
isEnabled.value = 'Y';
}, 500);
};
onMounted(() => {
getInitData();
});
</script>