vue 取v-model数据,类型是布尔,取消响应式
时间: 2023-11-17 09:09:41 浏览: 36
如果想要取出v-model数据,但是又不想让它具有响应式,可以使用如下方式:
1. 使用一个普通的变量来接收v-model的值,这样就不会具有响应式。
```
<template>
<div>
<input type="checkbox" v-model="checked">
<button @click="getCheckedValue">获取选中状态</button>
</div>
</template>
<script>
export default {
data() {
return {
checked: false
}
},
methods: {
getCheckedValue() {
const value = this.checked
console.log(value)
}
}
}
</script>
```
2. 使用$nextTick方法,将v-model的值复制到一个非响应式的变量中。
```
<template>
<div>
<input type="checkbox" v-model="checked">
<button @click="getCheckedValue">获取选中状态</button>
</div>
</template>
<script>
export default {
data() {
return {
checked: false,
nonReactiveValue: false
}
},
methods: {
getCheckedValue() {
const vm = this
vm.$nextTick(function () {
vm.nonReactiveValue = vm.checked
console.log(vm.nonReactiveValue)
})
}
}
}
</script>
```