4 ms·
>To answer to my initial question: API logic should not be written in Vuex nor in components. There is even a good example on how do that in some of the officia
by Tade0 8y ago
>To answer to my initial question: API logic should not be written in Vuex nor in components. There is even a good example on how do that in some of the official code examples.
Ok, but where's an example that shows how this backfires? Why should the API logic not be written in Vuex?
One more thing I think deserves mention: Vuetify - Vue's material design component set. It's being used in the project I'm currently in and I worked with Angular's material design component set and I have to say the former is way better built.
- pier25 8y ago> Ok, but where's an example that shows how this backfires? Why should the API logic not be written in Vuex? (Author here. I didn't really expand on that because the article was getting pretty long already.) For example, think about authentication logic. It involves more than just calling the API. Eg: extracting the JWT, saving it to localStorage, decoding it, maybe checking user roles, etc. If you do that kind of logic on Vuex actions then these will start being responsible for a lot of your application. If you are fine with that, by all means, use Vuex actions to do (a lot of) stuff not really related to managing state. Personally I don't think it makes any sense to delegate that kind of logic to Vuex in the same way I won't use Axios for anything other than making HTTP requests. There are also practical aspects. Vuex actions need to have a very particular signature, since you never really call them yourself. To me that was the final reason for moving a lot of logic to their own modules. In my latest projects I've almost stopped using Vuex actions altogether and it feels really good. Authentication is not managed by some parts of Vuex, but by my own auth module.
- philliphaydon 8y agoI use vuex with namespaced modules, but then those modules just call out to abstracted implementations if it's more than just http request. So for example with auth, I have a vuex auth module. It then has some getters which just get the state from the store, but the actions call to a separate module which manages storing it in LocalStorage or making the http requests. Works for me, but I just took what I learned (Vue) moved it to when I learned a year later (Vuex), when things became complicated in Vuex I just moved the complicated bits back 1 bit more and everything became simple again (which seemed like the logical thing to do).