How can vuex be used to dispatch events more elegantly?
黄舟
黄舟 2017-05-19 10:39:29
0
3
772

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?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
曾经蜡笔没有小新

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template