[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent

本文介绍了在Vue项目中如何避免直接修改通过props传递的值,并提供了一个具体的例子来展示正确的做法,即通过子组件触发事件通知父组件来更新状态。

Vue项目控制台报错如下:
[Vue warn]: 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: “allLocationOpen” found in…

错误原因:
避免直接改变属性,因为每当父组件重新渲染时,该值将被覆盖。相反,使用基于属性值的数据或计算属性。通过props传递给子组件的topActive,不能在子组件内部修改props中的topActive值。

代码示例:

<template>
<div>
<button @click="allLocationOpen = false">关 闭</button></div>
</template>
<script>
export default {
  name: 'AllLocation',
  props: {
    allLocationOpen: {
        type: Boolean,
        default: false
      },
  },
}
</script>

修改方案:不直接修改该参数,回调父组件方法去修改,避免子组件中直接修改props。

<template>
<div>
<button @click="$emit('editAllLocationOpen', false)">关 闭</button></div>
</template>
<script>
export default {
  name: 'AllLocation',
  props: {
    allLocationOpen: {
        type: Boolean,
        default: false
      },
  },
}
</script>
Vue 中,直接修改 prop 值出现 `[Vue warn]: Avoid mutating a prop directly...` 警告通常是因为在子组件中直接修改了父组件传递的 props 值。为避免该警告,可采用以下几种解决方案: 1. **使用事件机制**:通过 `$emit` 方法触发自定义事件,通知父组件进行数据修改。例如,子组件中触发事件: ```vue <template> <button @click="changeValue">修改值</button> </template> <script> export default { props: ['value'], methods: { changeValue() { this.$emit('update:value', newValue); } } } </script> ``` 父组件监听该事件并更新数据: ```vue <template> <ChildComponent :value="parentValue" @update:value="parentValue = $event" /> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentValue: '初始值' }; } } </script> ``` 2. **使用本地状态**:在子组件的 `data` 中定义一个本地状态变量,并将 `props` 的值复制给它,然后修改这个本地状态变量。示例如下: ```vue <template> <input v-model="localValue" /> </template> <script> export default { props: ['value'], data() { return { localValue: this.value }; } } </script> ``` 3. **使用计算属性**:通过计算属性的 `setter` 方法来修改本地状态。示例代码如下: ```vue <template> <input v-model="computedValue" /> </template> <script> export default { props: ['value'], computed: { computedValue: { get() { return this.value; }, set(newValue) { this.$emit('update:value', newValue); } } } } </script> ``` 4. **使用 `.sync` 修饰符**:在父组件中使用 `.sync` 修饰符简化数据同步过程。子组件触发更新事件: ```vue <template> <button @click="updateValue">更新值</button> </template> <script> export default { props: ['value'], methods: { updateValue() { this.$emit('update:value', newValue); } } } </script> ``` 父组件使用 `.sync` 修饰符: ```vue <template> <ChildComponent :value.sync="parentValue" /> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentValue: '初始值' }; } } </script> ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值