Skip to content

Commit c3a695e

Browse files
authoredNov 25, 2020
fix: fix getters stop working when component is destroyed (#1884)
1 parent 46dfe9c commit c3a695e

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed
 

‎docs/guide/getters.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ computed: {
1414

1515
If more than one component needs to make use of this, we have to either duplicate the function, or extract it into a shared helper and import it in multiple places - both are less than ideal.
1616

17-
Vuex allows us to define "getters" in the store. You can think of them as computed properties for stores. Like computed properties, a getter's result is cached based on its dependencies, and will only re-evaluate when some of its dependencies have changed.
17+
Vuex allows us to define "getters" in the store. You can think of them as computed properties for stores.
18+
19+
::: warning WARNING
20+
As of Vue 3.0, the getter's result is **not cached** as the computed property does. This is a known issue that requires Vue 3.1 to be released. You can learn more at [PR #1878](https://github.com/vuejs/vuex/pull/1883).
21+
:::
1822

1923
Getters will receive the state as their 1st argument:
2024

‎src/store.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { reactive, computed, watch } from 'vue'
1+
import { reactive, watch } from 'vue'
22
import { storeKey } from './injectKey'
33
import devtoolPlugin from './plugins/devtool'
44
import ModuleCollection from './module/module-collection'
@@ -286,15 +286,15 @@ function resetStoreState (store, state, hot) {
286286
store._makeLocalGettersCache = Object.create(null)
287287
const wrappedGetters = store._wrappedGetters
288288
const computedObj = {}
289-
const computedCache = {}
290289
forEachValue(wrappedGetters, (fn, key) => {
291290
// use computed to leverage its lazy-caching mechanism
292291
// direct inline function use will lead to closure preserving oldState.
293292
// using partial to return function with only arguments preserved in closure environment.
294293
computedObj[key] = partial(fn, store)
295-
computedCache[key] = computed(() => computedObj[key]())
296294
Object.defineProperty(store.getters, key, {
297-
get: () => computedCache[key].value,
295+
// TODO: use `computed` when it's possible. at the moment we can't due to
296+
// https://github.com/vuejs/vuex/pull/1883
297+
get: () => computedObj[key](),
298298
enumerable: true // for local getters
299299
})
300300
})

0 commit comments

Comments
 (0)
Please sign in to comment.