Skip to content

Switch 开关 ​

开关切换组件,基于 Vant Switch + Field 组件实现。

类型标识 ​

  • t: 'switch'

额外 Props ​

属性名类型默认值描述
switchPropsPartial<SwitchProps>-VanSwitch 组件的属性
switchOnobject{}VanSwitch 组件的事件
switchSlotsSwitchSlots-VanSwitch 组件的插槽

SwitchSlots ​

插槽名绑定值描述
nodeCommonSlotsProps自定义按钮的内容
backgroundCommonSlotsProps自定义开关的背景内容

Slots ​

支持所有 CommonSlots 插槽。

示例 ​

基础用法 ​

typescript
const formConfig = defineOption([
    {
        field: 'enabled',
        t: 'switch',
        label: '启用',
        defaultValue: false,
    },
]);

自定义开关样式 ​

typescript
const formConfig = defineOption([
    {
        field: 'enabled',
        t: 'switch',
        label: '启用',
        switchProps: {
            size: '28px',
            activeColor: '#07c160',
            inactiveColor: '#ee0a24',
        },
    },
]);

自定义按钮内容 ​

tsx
const formConfig = defineOption([
    {
        field: 'enabled',
        t: 'switch',
        label: '启用',
        switchSlots: {
            node: () => <VanIcon name="success" />,
        },
    },
]);