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

本文介绍了一个常见的Vue警告,即直接修改Prop值导致的问题,并提供了解决方案。使用ElementUI版本2.15.8时可能会遇到此警告,通过降级ElementUI版本可以消除该警告。

[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: “placement”

element ui版本问题

报错提示:vue.runtime.esm.js?2b0e:619 [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: “placement”

found in

—> at packages/date-picker/src/picker.vue
at src/views/demo/index.vue
at src/App.vue

解决方案:我使用的 ElementUI 版本是 “element-ui”: “^2.15.8”,会出现这个报错提示,更换一个更低点的版本即可解决报错,不解决报错好像也不影响效果展示

// 卸载 elementUI 旧版本
npm uninstall element-ui
// 安装新版本
npm i element-ui -S
// 安装指定版本 
npm i element-ui@2.15.7 -S
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、付费专栏及课程。

余额充值