나는 켜기/끄기를 전환하는 vue 스위치를 사용하고 있는데 어떤 상태인지 알고 싶습니다. https://www.npmjs.com/package/vue-switch의 설명서에 따르면 나는 다음을 사용해야합니다 : value.sync = " toggle "이라는 데이터 속성을 사용하여 스위치 구성 요소에서 'toggle'을 호출합니다.바인드 데이터 값에 대한 값 전환
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "value" vue switch found in ---> <Switcher> at E:\Dev\BackgroundCheck.Members\front-end\node_modules\vue-switch\switch.vue
내 HTML :
<switcher size="lg" color="green" :value.sync="toggle"></switcher>
내 JS :
<script>
import mySwitch from 'vue-switch';
export default {
name: 'BackgroundReports',
components: {
'switcher': mySwitch
},
computed: {
report() { return this.$store.getters.currentReport },
isBeingMonitored() { return this.$store.getters.isBeingMonitored }
},
mounted() {
this.toggle = this.isBeingMonitored;
}
}
</script>
switch.vue 코드 :
<template>
<div :class="className" @click="onClick">
<span class="open">{{ openName }}</span>
<span class="close">{{ closeName }}</span>
</div>
</template>
<script>
'use strict';
export default {
props: {
value: {
default: true,
twoWay: true
},
size: {
type: String,
default: 'md中'
},
// blue red green orange
color: {
type: String,
default: 'red'
},
openValue: {
default: true
},
closeValue: {
default: false
},
openName: {
type: String,
default: 'ON'
},
closeName: {
type: String,
default: 'OFF'
},
disabled: {
type: Boolean,
default: false
}
},
computed: {
className() {
let {
value,
openValue,
closeValue,
size,
color,
disabled
} = this;
return {
'vue-switch': true,
'z-on': value === openValue,
'z-disabled': disabled,
['s-' + size]: true,
['c-' + color]: true
};
}
},
methods: {
onClick() {
let {
disabled,
value,
openValue,
closeValue
} = this;
if (!disabled) {
if (openValue === value) {
this.value = closeValue;
} else {
this.value = openValue;
}
}
}
}
}
</script>
나의 추측은 이런 종류의 것이 괜찮은 Vue 1을 위해 vue-switch라고 쓰여졌습니다. –
@RoyJ crap ... 입력을 주신 덕분에 – Linx
Vue 2에서 작동하게하려면'value'에 할당 된 줄을'$ way'에 따라'$ emit'하는 줄로 바꾸면됩니다. sync' 작동 중] (https://vuejs.org/v2/guide/components.html#sync-Modifier) –