基础模式

<template>
 <y-switch></y-switch>
 <y-switch v-model="switchValue"></y-switch>
 <y-switch :modelValue="switchValue2" @change="handleValueChange"></y-switch>
</template>

<script>
import { defineComponent, ref } from "vue";

export default defineComponent({
 name: "Base",
 setup() {
   const switchValue = ref(false);
   const switchValue2 = ref(true);
   const handleValueChange = value => {
     console.log('value:', value)
     switchValue2.value = value;
   }
   return {
     switchValue,
     switchValue2,
     handleValueChange,
   };
 }
});
</script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

禁用态

<template>
 <y-switch disabled></y-switch>
 <y-switch :modelValue="true" disabled></y-switch>
</template>

<script>
import { defineComponent } from "vue";

export default defineComponent({
 name: "Disabled",
});
</script>

1
2
3
4
5
6
7
8
9
10
11
12
13

不同尺寸

<template>
 <y-switch size="large"></y-switch>
 <y-switch size="normal"></y-switch>
 <y-switch size="small"></y-switch>
 <y-switch size="large" active-text="" inactive-text=""></y-switch>
 <y-switch size="normal" active-text="" inactive-text=""></y-switch>
 <y-switch size="small" active-text="" inactive-text=""></y-switch>
</template>

<script>
import { defineComponent, ref } from "vue";

export default defineComponent({
 name: "Base",
 setup() {
   const switchValue = ref(false);
   const switchValue2 = ref(true);
   const handleValueChange = value => {
     console.log('value:', value)
     switchValue2.value = value;
   }
   return {
     switchValue,
     switchValue2,
     handleValueChange,
   };
 }
});
</script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

自定义标签

off
晚上
早睡早起好精神
<template>
 <y-switch active-text="on" inactive-text="off"></y-switch>
 <y-switch active-text="" inactive-text=""></y-switch>
 <y-switch active-text="早晨" inactive-text="晚上"></y-switch>
 <y-switch active-text="一日之计在于晨" inactive-text="早睡早起好精神"></y-switch>
</template>

<script>
import { defineComponent } from "vue";

export default defineComponent({
 name: "Custom",
});
</script>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

API

Switch props

PropertyDescriptionTypeAccepted ValuesDefault
modelValuethe value of switch, bind with v-modelBoolean--
disabledwhether the switch is disabledBoolean-false
sizethe size of switchStringlarge/normal/smallnormal
active-textthe text when switch onString--`
inactive-textthe text when switch offString--

Switch events

EventDescriptionParameters
changethe change event of switchcurrent value
update:modelValuethe v-model bind event of switchcurrent value