Getting avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders

前端 未结 2 1211
花落未央
花落未央 2021-01-22 21:57

I am new to vuejs and I am trying to sync active data to parent but getting an error as

vue.js:523 [Vue warn]: Avoid mutatin

2条回答
  •  无人及你
    2021-01-22 22:15

    As the error suggests, you are trying to change one of the props active.

    Prop being mutated: "active" (found in component )

    As props are dynamically sent from parent, they will change whenever parent changes those, if you change them in child also, there will be conflict, thats why you are getting this error.

    As par the documentation:

    the parent-child component relationship can be summarized as props down, events up. The parent passes data down to the child via props, and the child sends messages to the parent via events.

    So the proper way to do this will be to emit an event, which will call a method in parent and change the variable active in parent where it is defined. Following will be the code changes:

    
    
    

提交回复
热议问题