使用props进行父子组件通信时产生错误: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: “currentIndex”
产生错误的原因:在子组件中,你试图修改父组件通过props与子组件通信的数据。
两种解决方法:
- 将这个父组件传进来的要修改的数据,通过$emit再传回给父组件,在父组件中进行修改。
- 当父组件通过props与子组件通信时,可以将数据放在一个对象中传递给子组件,这时再子组件就可以修改对象中的属性,也就修改了父组件传过来的数据。
例如(方法一):
父组件:
<tab-control
:titles="titles"
@choseItem="choseItem"
:currentIndex="currentIndex"
ref="tabControl"
></tab-control>