Quasar is great. But, it is also a bit different when it comes to accessing the Vuex store from your code.
Why is Quasar any different?
“Is it not just Vue?”
Well, yes. But.. the project structure created by Quasar CLI differs from a “normal” that by Vue CLI. Quasar CLI builds on top of Vue as well but may not follow similar practices to other frameworks / libraries using Vue.
If you can’t really move away from the standard structure, just use Quasar as a plugin in a project created by Vue CLI. You may lose some features, but you may find yourself at home and stay happy. In that case, you could leverage your existing knowledge on using Vuex states and mutations in multiple ways.
Or.. you could just experiment more and accept a different normal.
An app created by Quasar CLI does not have main.js
. Vue compile/load sequence is taken care by quasar with options defined in a /quasar.conf
file.
The store/index.js
file in Vue CLI will have a function invocation -
|
|
This store is then used in main.js
..
|
|
This and the corresponding setup within Vue CLI projects make it convenient to access states or mutations (in addition to helper functions).
-
Use
this.$store
in components, store modules, or router -
Directly import
store
and use it to access store functions1 2 3 4 5 6
import store from "./store"; // ... if (!store.state.auth.isLoggedIn) { // how do you penalise a person without login }
In Quasar CLI, the /store/index.js
file returns a store
object..
|
|
… but this is not used in the same way as Vue CLI.
Also, the modules are structured within folders in /src/store
by default. And.. you can use quasar CLI to create a new module.
Directly accessing store will not work -
-
Can’t just import store
1 2 3 4
import store from "./store"; store.set("blah", val); // store is a function and does not support `set`
-
this.$store
isundefined
and not available in stores / modules
How can you use a Vuex store in Quasar?
You have more than one way to get to the store..!
Use helper functions
Continue to use mapState
, mapMutations
, and friends to manage your state.
Use Pathify
Works great everywhere. Use sync
in store modules or in components. See working with Pathify.
Use “normal” commit
Fall back on the trusty commit in store modules!
|
|
Change the default store structure
Just change the Vuex store structure to access it from anywhere.
Modify ./src/store/index.js
.
|
|
This is useful to access state as well as commits.
|
|
Use boot
time plugins (“app plugins”) in Quasar
Create a new plugin (say superfunc
) by creating a new file in /src/boot/superfunc.js
-
|
|
Then happily use superfunc
in places -
|
|
Parting Words
Not quite what you call “super fun”, but stores simply work in Quasar, and that’s all there is to it.