我正在try 使用一个来自v-modelprops 的数据,下面的代码可以工作,但有一个警告.

<template>
<div>
       <b-form-input v-model="value" @change="postPost()"></b-form-input>
</div>
</template>
<script>
    import axios from 'axios';
    export default {
        props: {
            value: String
        },
        methods: {
            postPost() {
                axios.put('/trajectory/inclination', {
                    body: this.value
                })
                    .then(response => {
                    })
                    .catch(e => {
                        this.errors.push(e)
                    })
            }
        }
    }
</script>

警告说:

"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"

所以我改变了,现在我使用的是一个警告所说的数据.

<template>
<div>
       <b-form-input v-model="_value" @change="postPost()"></b-form-input>
</div>
</template>
<script>
    import axios from 'axios';

    export default {
        props: {
            value: String
        },
        data() {
            return {
                _value: this.value
            }
        },
        methods: {
            postPost() {
                axios.put('/trajectory/inclination', {
                    body: this._value
                })
                    .then(response => {
                    })
                    .catch(e => {
                        this.errors.push(e)
                    })
            }
        }
    }

所以现在代码不起作用了,警告说:

"Property or method "_value" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option"

知道如何修复第一个代码来 suppress 警告吗?(或者关于如何修复第二个代码的一些 idea ?)

注意事项:b-form-input这不是我的组件,这是Boostrap Vue(Doc for b-form-input)的文本输入

推荐答案

伯特解决了你的直接问题,但我认为你也应该知道你的方法有点不合适.由于最终要将新值发送到postPost,因此实际上不需要修改本地副本.使用发送到change处理程序的event对象从输入中获取当前值.

使用:value而不是v-model,在指定change处理程序时不要包含调用括号.

<template>
<div>
       <b-form-input :value="value" @change="postPost"></b-form-input>
</div>
</template>
<script>
    import axios from 'axios';
    export default {
        props: {
            value: String
        },
        methods: {
            postPost(event) {
                axios.put('/trajectory/inclination', {
                    body: event.target.value
                })
                    .then(response => {
                    })
                    .catch(e => {
                        this.errors.push(e)
                    })
            }
        }
    }
</script>

Vue.js相关问答推荐

在VueJS中卸载元素之前是否应该删除JavaScript事件收件箱?

为什么在Vue.js中修改非react 性似乎是react 性的?

Nuxt 2.15.7 的构建错误 - 无法解析 CSS @font-face URL

Vue,Composition API:如何从react 式转换中跳过深度嵌套的属性?

在 Vue 项目之间共享assets和组件

正确使用 Vue $refs

如何在异步功能上使用 debounce?

将检测外部点击自定义指令从 Vue 2 迁移到 Vue 3

带有 vue.js 的 img 网址

使用 Vue Router 设置页面标题

VueJS 禁用特定属性的react性

使用 Vee-Validate 在提交时验证子输入组件

如何在 Node.js 服务器上部署 Vue.js 应用程序

如何在 NPM 上创建和发布 Vuejs 组件

Vue 组件 Vue-Instant

我在 Nuxt 2.14.0 中运行的是什么版本的 Vue?

在组件之间共享数据

如何使用 JavaScript 按索引删除数组元素?

beforeCreate 挂钩中的 Vue 2.1 调用方法不起作用

vuex 是 state.array.push react式的吗?