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.
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 >