I know you can disable caching in Vue computed properties by including a cache: false
option. For example:
computed: {
now: {
cache: false,
get() {
return Date.now();
}
}
}
But I was wondering if this feature was available for Vuex getters
To "disable" caching, you could take advantage of the fact that getters which are to be used as functions are not cached.
From the vuex docs
Note that getters accessed via methods will run each time you call them, and the result is not cached.
If you don't mind adding parenthesis (actually, calling functions) when accessing your non-cached property you could use something like this:
getters: {
myNonCachedGetter: state => () => {
// original getter body
}
}
It is then used as myNonCachedGetter()
.
It turns out that the following doesn't work in newer versions of vuex.
However, if you would like it to look as a normal getter, you could wrap it into an function with invocation:
getters: {
myNonCachedGetter: state => (() => {
// original getter body
})()
}
It does make the code look a bit harder to read, but it can be called as simple as myNonCachedGetter
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With