Skip to content

Commit 3cd2fbe

Browse files
committed
将switch的状态通过props传进来
1 parent 02b0af7 commit 3cd2fbe

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/components/SwitchDemo.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<template>
22
<div>
3-
<Switch />
3+
<Switch :value="y" @input="y = $event" />
44
</div>
55
</template>
66
<script lang="ts">
77
import Switch from "../lib/Switch.vue"
8+
import {ref} from 'vue';
89
export default {
9-
components:{ Switch }
10+
components:{ Switch },
11+
setup(){
12+
const y = ref(false)
13+
return { y }
14+
}
1015
}
1116
</script>

src/lib/Switch.vue

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<template>
2-
<button @click="toggle" :class="{checked}"> <span></span> </button>
2+
<button @click="toggle" :class="{checked:value}"> <span></span> </button>
3+
<div>{{ value }}</div>
34
</template>
45
<script lang="ts">
5-
import { ref } from 'vue'
66
export default {
7-
setup(){
8-
const checked = ref(true)
7+
props:{
8+
value:Boolean
9+
},
10+
setup(props,context){
911
const toggle = ()=>{
10-
checked.value = !checked.value
12+
context.emit('input', !props.value)
1113
}
12-
return { checked,toggle }
14+
return { toggle }
1315
}
1416
}
1517
</script>
@@ -23,6 +25,7 @@ button{
2325
background: grey;
2426
border-radius: $h/2;
2527
position: relative;
28+
cursor: pointer;
2629
}
2730
span{
2831
position: absolute;

0 commit comments

Comments
 (0)