Vue: Reasons to use props instead of referencing parent data?

后端 未结 1 1610
眼角桃花
眼角桃花 2020-12-18 15:08

In VueJS, I have seen different ways of accessing parent properties from a component. Say I want to use the parent property items in my component.

相关标签:
1条回答
  • 2020-12-18 15:35

    The props should be mutated in the parent component, according to the official doc :

    All props form a one-way-down binding between the child property and the parent one: when the parent property updates, it will flow down to the child, but not the other way around. This prevents child components from accidentally mutating the parent’s state, which can make your app’s data flow harder to understand.

    In addition, every time the parent component is updated, all props in the child component will be refreshed with the latest value. This means you should not attempt to mutate a prop inside a child component. If you do, Vue will warn you in the console

    So in order to update props from child component you should use this.$emit event and send the new value in order to handle the update in the parent one.

    0 讨论(0)
提交回复
热议问题