Vue2中使用element-ui的el-data-picker报错Avoid mutating a prop directly ...

492 阅读1分钟

报错信息

[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 ...

image.png

原因

锁定组件,发现是 el-date-picker 组件抛出的警告。通过在 github 上搜索,最终找到了答案。
问题出在了这个 PR #21806 增加了 props placement 用来适应位置,但是之前的代码 created 时有给 placement 赋值。
this.placement = PLACEMENT_MAP[this.align] || PLACEMENT_MAP.left;
说白了之前 placement 是 data 的对象,现在变成 props 了,然后修改就报错了。

解决方法

想要解决这个问题,可以修改版本到 2.15.8或更低版本。
npm i element-ui
npm i element-ui@2.15.8
image.png
注意:这里不要【‘^’】,否则还会报错