Answer the question
In order to leave comments, you need to log in
Vuex best practices or how to do it right?
There is a state with a certain set of properties
prop1: 0,
prop2: "",
prop3: [],
Answer the question
In order to leave comments, you need to log in
All data in state should be logically divided into modules. All commits must also be logically separated within each module. Personally, I prefer not to create a root store at all - everything is in submodules.
As for validation, Aetae wrote everything correctly .
Personally, I use the following approach. It is not the best, and not always suitable.
- All components call only action
- Commits are called only inside and action after all checks and validations.
- All commits are designed to receive obviously correct data.
Thus:
mutation is a low-level tool for managing storage.
action is a high-level storage management manager that checks all data, performs any additional manipulations, transformations, splits large data into parts if necessary, and so on, and only then transfers ready-made data to mutation.
component is a store user. Which does not cause mutation directly, but only causes action. And in exceptional cases, it can get direct access to the repository, bypassing any checks and changes by causing a direct mutation.
Everything should be within the logical, not technical division, this is not only about Vue.
If you are validating the input, it is part of the input and has nothing to do with the state. If you're validating third party data - that's part of "getting third party data", etc.
It is the same with changing the state - if one logical operation requires changes to several elements of the state, then it must be performed by one operation.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question