Vuex is an awesome state management library. It's simple and integrates well with Vue. Why would anyone leave Vuex? The reason can be that the upco...
For further actions, you may consider blocking this person and/or reporting abuse
Hey,
Nice article!
I've been investigating similar functionality recently, and wrapped up shared stores into a class-based format, that works in Vue 2, 3 and Nuxt and supports state, getters, watches, actions (methods) and inheritance:
github.com/davestewart/vue-class-s...
The library is a single decorator that allows you to use a single interface (classes) and returns you a working store in Vue 2 (as a new Vue) or Vue 3 (using the Reactivity API).
Check the docs for provide/inject example and demos for working code:
github.com/davestewart/vue-class-s...
Nice work, looks amazing 👍
One idea: add an example on how to use it as a plugin...or publish a plugin also in the package
Thanks! Can you expand on that?
Just an example on the Readme on how to use it with the
app
instancesuse
method.app.use(store)
There's no
use
per-se, you just create the models and use them where you need them.If you want a "global" style example, either import as needed or use inject exactly as you have (example code in the README):
github.com/davestewart/vue-class-s...
What is the benefit of
provide/inject
compared to directlyimport { state }
?The second
import
solution creates a tightly coupled store, which makes testing, using another store with the same interface, using different stores in different parts of the application hard.Both solutions work. The second one is simpler but hardly coupled to the implementation. The first one is a bit more complex but makes the code more open to alteration.
This is why one must try their best to ensure that with a tightly coupled store, such stores are only used at top level components, leaving the rest to be dumb components used only to display data or emit an event
Vue2 (2.6+) supports similar functionality via Observable.
The downside of switching to alternatives for managing state is the loss of ability to easily debug with the Vue browser extension (the vuex tab specifically). I would caution against using this large scale apps with multiple people working on it, since the conventions around vuex make team and large/long-term dev easier, but for simple apps, I think vuex can easily be replaced with something that may have a simpler learning curve.
Yes, the pro of using existing well-known tools are conventions and that people know it. You can come up with your own conventions and integrate it with Vue devtools also, but yes, it's extra work.
That's pretty sweet! Simpler than Vuex.
I think I'll use
provide
andinject
for simple states, it's perfect.But how can we do a cache system like vuex persist?
I would use the
watchEffect
function for persistence.It's more simple until you trip up on something and waste your time going down rabbit-holes.
Trip up on what? It's the built-in reactivity system with functions mutating the variables.
I was talking about the simple implementation of trying to replace Vuex, it's not a complete solution. Still it is a nice write up on how one would go about using a simple reactive global store.
I have yet to see what real benefits Vuex or in general a Flux-based solution is providing compared to such simple solutions. In my eyes it's more flexible and less boilerplate heavy. If you need more features one can always write up logic around that specifically for the usecase (e.g. what we currently have a model manager that takes care of model instantiation, persistence etc.).
The only really thing that I currently see where vuex has advantages is vue dev tools integration.
Do you have examples where vuex shines compared to that solution?
I agree with this, I'm writing a really large vue2 + vuex + ts SPA and thought that vuex would be obligatory, turns out making a really strict typed store with vuex 3 and a lot of store modules is possible but infuriating, its 10 times the boilerplate and whenever you use your store on a component vuex doesn't infer the typing due to its weird string accessor of getters, state and what not.
After fighting against the lib lack of proper TS support i decided to test some libs like vuex-class-modules, vuex-module-decorators and many others, every single one had annoying issues or lacked features.
Looking back, the only thing vuex has that is awesome is the community plugins and the devtools debugger, but i don't think its worth it, with the new composition API i don't see much reason to use it, especially since you can enforce your own accessors and practices instead of the weird soup that is vuex
Great article! I just finished converting a project to use this approach instead of Vuex so that we can use TypeScript. The only downside is you lose the Vuex tools in the Vue devtools extension. Once I upgrade to native Vue 3 instead of the composition API plugin, I might switch back to Vuex just to get the benefits back from the devtools.
Hello, is there any real-world practical example for this ? I'm struggling with non-opinionated approach, so there might be a convenience way of adopting this approach
I haven't done any official project with this approach, but it was completely fine for a TodoMVC app. If you go down this way you can take general convenience ways from vuex and create your own.
Thanks. Wrote my own project using this approach - github.com/gintsgints/quarkus-full...
Just used typescript class and private. May be you can review give suggestions :)
Seems good to me, great work :) . What I would work on is the naming of variables like
resp1
to give them more meaning/make them more readable.That ist what i search :) Nice article thanks for it