让前端飞Web前端之路

Avoid mutating a prop directly s

2020-10-28  本文已影响0人  嘻哈章鱼小丸子

Vue项目中,使用Element-UI Dialog对话框组件,关闭弹框时弹出如下警告:

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

意思是子组件dialog里面直接修改了来源于父组件的visible属性,这是不合法的。

dialog使用方式如下:
dialog
由于visible使用了sync修饰符(双向绑定),查了下官网dialog API,解决办法有如下2种:

父组件:

父组件
或简写为:
<add-pack v-if="visible" :visible.sync="visible"></add-pack>

子组件:
dialog提供before-close作为关闭前的回调,在这里触发事件就好了:

before-close
close

总之,一句话:不要在子组件里修改父组件的状态。如果层级太深,不好处理,放store。

上一篇 下一篇

猜你喜欢

热点阅读