vue.js - 防止 Vue 中的 v-model 更改

标签 vue.js vuejs2

我在 data() 方法的数组中存储了一个任务列表。

当对象通过 v-model 指令链接时,有没有办法可以阻止/逆转对对象的更改?

例如(在组件中):

<template>
    <tr v-for="task in tasks" :key="task.id">
        <input @change="complete(task)" v-model="task.complete" type="checkbox" name="complete">

        <td>{{ task.summary }}</td>
    </tr>
</template>

<script>
export default {
    props: ['allowChanges'],

    data() {
        return {
            tasks: [],
        };
    },

    methods: {
        complete(task) {
            if (this.allowChanges) {
                // axios.post() etc.
            } else {
                // This doesn't work:
                task.complete = !task.complete
            }
        },
    }
}
</script>

我尝试过使用观察程序和方法,但似乎无法撤消/取消使用 v-model 指令进行的更改?

最佳答案

当不允许更改时禁用输入。

<input @change="complete(task)" v-model="task.complete" type="checkbox" name="complete" :disabled="!allowChanges">

关于vue.js - 防止 Vue 中的 v-model 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45947363/

相关文章:

javascript - 在 Vue 模板中按类别组循环遍历产品项

javascript - Vuex commit 不触发 v-show

javascript - 如何在 VueJS 中获取渲染列表项的 innerText

javascript - Vue2 : JSON object using emit not working?

javascript - 渲染错误 : "TypeError: Cannot read property ' first_name' of undefined"

jquery - 如何在 vue.js 应用程序中使用 jQuery 脚本?

javascript - 无法在 vue-select 上触发事件

vue.js - VueJS : Computed Property Is Calculated Before Created in Component?

vue.js - 组件上触发代码 v-show=true

node.js - 当我在 Laravel 项目中运行 npm install 时,它显示错误 - npm WARN deprecated gulp-util@3.0.8 : gulp-util is deprecated - replace it