[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent..
•
前端
![[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent..](/img/a4/ebb66d31530743eea1ab4a9068832675.png)
错误:[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: “value”.
译文:[Vue警告]:避免直接改变prop,因为每当父组件重新呈现时,该值将被覆盖。相反,应该使用基于prop值的数据或计算属性。prop发生变异:”value”。简单地说,不可以直接修改props接收的属性值。
解决方法:只需要在子组件的data()重新定义一个变量,将props属性赋值给子组件的新定义的变量,可用 属性.sync 将数据同步更新到父组件
原来的写法:
父组件
![[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent..](/img/8a/b3ba1fb724d2494a9f53426dfddea079.png)
子组件
![[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent..](/img/fe/7913297be6854f5ea0cb73129200d10c.png)
![[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent..](/img/ca/426f220987954b11a25f8a551ddc7157.png)
修改后,父组件不变,子组件:
![[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent..](/img/d6/911b9d2f5070401bb04d91857cde6931.png)
![[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent..](/img/af/e5e24233ad294d7d948a6874c928ecca.png)
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/c88b5279e3.html
