Switch 开关
type : 'switch'
用于在打开和关闭状态之间进行切换。
代码演示
基础用法
<template>
<div class="">
<x-form ref="formRef" :model="formValue" :items="formOptions" />
<div style="margin:10px">
<van-button @click="onSubmit" block type="primary">验证</van-button>
</div>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
const formRef = ref();
const formValue = ref<Indexable>({
input: '',
});
const formOptions = ref([
{
type: 'switch',
label: 'Switch',
name: 'switch',
placeholder: '请输入内容',
required: true,
attrs: {
'active-value': '0',
"inactive-value": '1',
}
},
]);
const onSubmit = () => {
formRef.value?.validate().then(() => {
console.log('submit values =', formValue.value);
}).catch((error: any) => {
console.log('error', error);
})
}
</script>
<style lang="scss" scoped></style>
基础用法
API
Props
与官方组件 Switch 开关 参数保持一致,这里以展示新增属性配置为主,具体配置参见官方文档。