在使用vue進行前端開發(fā)時,可能會遇到循環(huán)渲染input輸入框的需求,當使用v-for循環(huán)后,對v-model進行值的綁定時,可能會出現(xiàn)以下錯誤,如圖所示:
v-model cannot be used on v-for or v-slot scope variables because they are not writable.
錯誤代碼:
<template v-for="(item, index) in dataArray" :key="index">
<el-form-item>
<el-input v-model="item" />
</el-form-item>
<el-form-item>
<el-input v-model="item" />
</el-form-item>
</template>
通過查閱文檔發(fā)現(xiàn),v-model 不可以直接修改 v-for 循環(huán)迭代時別名上的數(shù)據(jù)
解決方法:
我們可以使用對象的索引來進行v-model的值的綁定。文章來源:http://www.zghlxwxcb.cn/news/detail-594903.html
<template v-for="(item, index) in dataArray" :key="index">
<el-form-item>
<el-input v-model="dataArray[index]" />
</el-form-item>
<el-form-item>
<el-input v-model="dataArray[index]" />
</el-form-item>
</template>
通過以上的方法就可以完美解決了。文章來源地址http://www.zghlxwxcb.cn/news/detail-594903.html
到了這里,關于解決:v-model cannot be used on v-for or v-slot scope variables because they are not writable.報錯問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!