I have always used new Vue() to define a global communication Bus, so that communication between components only requires one side to emit and the other side to listen on. Later, I switched to vuex. I feel that vuex is just a global data. When an event needs to be dispatched: for example, an event of component A triggers a method of component B (for example, click a button, cancel a timer, etc.) , at this time vuex seems very useless. The method I use now is to define a Boolean value, take false every time it is triggered, and then watch this value in the component and call the method.
As shown in the picture:
Then listen:
Does anyone have a better way?
Isn’t it good to use vuex for this? How come it seems useless? Similar to this, use loginSuccess to control whether to display the user's avatar. In this way, I think it is quite pleasant to use
1. If you just want to notify an object to change its state, it is recommended to use events
1. Event communication between parent and child components, by listening to the corresponding event name in the parent component and triggering the child component.
2. Non-parent-child components use Event Bus method
2. Data sharing between components
1. Medium and large applications use vuex
2. When there are relatively few application scenarios, parent-child components use props, and non-parent-child components use Event Bus to transfer data