Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

vue单向数据流

直接在子组件中修改父组件传来的值会报错,我想知道如果vue支持这么干,会有什么后果?

直接修改报错

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.

大概意思是说如果在子组件中修改,当父组件重新渲染时会覆写(是覆写不是重写)掉那个值,

如果我理解的没错的话?

这意思是父组件重新渲染会再一次把值传到子组件,然后会把子组件修改过的值覆盖掉吗?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你理解的没错。另外就是单向的好处就是修改的地方局限在组件内的话也利于追踪bug,不然你子组件可以任意更改prop,那么将会对查找问题带来麻烦,你怎么知道是哪里的修改导致的?一层层找?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...