vue3 雙向綁定 失效


錯誤方式(復選框與數組綁定
<template>
<!-- 復選框?-->
<div>
<input type="checkbox" id="jack" value="Jack" v-model="checkedNames.arr" />
<label for="jack">Jack</label>
<input type="checkbox" id="john" value="John" v-model="checkedNames.arr" />
<label for="john">John</label>
<input type="checkbox" id="mike" value="Mike" v-model="checkedNames.arr" />
<label for="mike">Mike</label>
<br />
<span>Checked names: {{ checkedNames.arr}}</span>
</div>
</template>
<script setup>
import {reactive} from "vue";

const checkedNames = reactive([])

</script>

 

 正確方式

<template>
<!-- 復選框?-->
<div>
<input type="checkbox" id="jack" value="Jack" v-model="checkedNames.arr"/>
<label for="jack">Jack</label>
<input type="checkbox" id="john" value="John" v-model="checkedNames.arr"/>
<label for="john">John</label>
<input type="checkbox" id="mike" value="Mike" v-model="checkedNames.arr"/>
<label for="mike">Mike</label>
<br/>
<span>Checked names: {{ checkedNames.arr }}</span>
</div>
</template>

<script setup>
import {reactive} from "vue";

const checkedNames = reactive({
arr:[]
})

</script>

<style scoped>

</style>

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM