When should I use Vuex?

前端 未结 4 719
梦毁少年i
梦毁少年i 2020-12-07 00:02

Now I start to learn vue, and I\'m creating SPA for editing database. Now I can\'t understand where I should use a Vuex. I can use props and $emit everywhere an

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 01:00

    The main purpose of using Vuex (which is Vue's version of Redux) is state (or data) management. In layman's terms, it's having a single source of truth (for your data).

    It opposes the common pattern of emitting an event when data changed so other parts of the application can keep their data in sync with the current component's data.

    Rather than allowing one particular component to change the data directly, you delegate the change to a separate module: the store. Any component using that data will be notified about the change. This becomes extremely useful in complex applications, where the common emit pattern runs the risk of creating cyclic update loops.

    Apps using the Redux pattern benefit from:

    • predictability of outcome,
    • ease of testing
    • code maintainability and scalability
    • separation of concerns
    • ease of debugging (time travel: the ability to undo or replay a particular change for debugging purposes).

    In short, it gives developers control and confidence, as it makes understanding and modifying complex code, data or app behavior significantly easier.


    Like any other pattern (or system, or tool), the Redux pattern can be over-used. It should be obvious you don't have to keep data that's only used by one component in the store. Since no other component needs to know about any changes to that data, one should manage it in the component using it.

    Vuex allows organisation of data into modules so you can keep everything tidy and maintainable.

    Last, but not least, one of the most useful benefits of using the Redux pattern is making SSR (server side rendering) implementation a breeze. SSR greatly improves optimization and user experience and increases the perceived performance of the application.

提交回复
热议问题