Skip to content

Commit de2070d

Browse files
committed
also expose getters inside getters
1 parent 870bdc5 commit de2070d

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

examples/counter/Counter.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div>
3-
Clicked: {{ count }} times
3+
Clicked: {{ $store.state.count }} times, count is {{ evenOrOdd }}.
44
<button @click="increment">+</button>
55
<button @click="decrement">-</button>
66
<button @click="incrementIfOdd">Increment if odd</button>
@@ -13,7 +13,7 @@ import { mapGetters, mapActions } from 'vuex'
1313
1414
export default {
1515
computed: mapGetters([
16-
'count'
16+
'evenOrOdd'
1717
]),
1818
methods: mapActions([
1919
'increment',

examples/counter/store.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ const actions = {
4343
}
4444
}
4545

46+
// getters are functions
47+
const getters = {
48+
evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd'
49+
}
50+
4651
// A Vuex instance is created by combining the state, mutations, actions,
47-
// and getters. Components should prefer interacting with the store via
48-
// getters and actions.
52+
// and getters.
4953
export default new Vuex.Store({
5054
state,
51-
getters: {
52-
count: state => state.count
53-
},
55+
getters,
5456
actions,
5557
mutations
5658
})

src/index.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ function initStoreState (store, state, getters) {
246246
Object.keys(getters).forEach(key => {
247247
const fn = getters[key]
248248
// use computed to leverage its lazy-caching mechanism
249-
computed[key] = () => fn(store._vm.state)
249+
computed[key] = () => fn(store)
250250
Object.defineProperty(store.getters, key, {
251251
get: () => store._vm[key]
252252
})
@@ -265,27 +265,40 @@ function initStoreState (store, state, getters) {
265265
}
266266

267267
function extractModuleGetters (getters = {}, modules = {}, path = []) {
268-
if (!modules) return getters
268+
if (!path.length) {
269+
wrapGetters(getters, getters, path, true)
270+
}
271+
if (!modules) {
272+
return getters
273+
}
269274
Object.keys(modules).forEach(key => {
270275
const module = modules[key]
271276
const modulePath = path.concat(key)
272277
if (module.getters) {
273-
Object.keys(module.getters).forEach(getterKey => {
274-
const rawGetter = module.getters[getterKey]
275-
if (getters[getterKey]) {
276-
console.error(`[vuex] duplicate getter key: ${getterKey}`)
277-
return
278-
}
279-
getters[getterKey] = function wrappedGetter (state) {
280-
return rawGetter(getNestedState(state, modulePath), state)
281-
}
282-
})
278+
wrapGetters(getters, module.getters, modulePath)
283279
}
284280
extractModuleGetters(getters, module.modules, modulePath)
285281
})
286282
return getters
287283
}
288284

285+
function wrapGetters (getters, moduleGetters, modulePath, force) {
286+
Object.keys(moduleGetters).forEach(getterKey => {
287+
const rawGetter = moduleGetters[getterKey]
288+
if (getters[getterKey] && !force) {
289+
console.error(`[vuex] duplicate getter key: ${getterKey}`)
290+
return
291+
}
292+
getters[getterKey] = function wrappedGetter (store) {
293+
return rawGetter(
294+
getNestedState(store.state, modulePath), // local state
295+
store.getters, // getters
296+
store.state // root state
297+
)
298+
}
299+
})
300+
}
301+
289302
function enableStrictMode (store) {
290303
store._vm.$watch('state', () => {
291304
assert(store._committing, `Do not mutate vuex store state outside mutation handlers.`)
@@ -301,7 +314,9 @@ function isPromise (val) {
301314
}
302315

303316
function getNestedState (state, path) {
304-
return path.reduce((state, key) => state[key], state)
317+
return path.length
318+
? path.reduce((state, key) => state[key], state)
319+
: state
305320
}
306321

307322
function install (_Vue) {

test/unit/test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,18 +486,20 @@ describe('Vuex', () => {
486486

487487
it('module: getters', function () {
488488
const makeGetter = n => ({
489-
[`getter${n}`]: (state, rootState) => {
490-
if (rootState) {
491-
expect(rootState).to.equal(store.state)
492-
}
489+
[`getter${n}`]: (state, getters, rootState) => {
490+
expect(getters.constant).to.equal(0)
491+
expect(rootState).to.equal(store.state)
493492
return state.a
494493
}
495494
})
496495
const store = new Vuex.Store({
497496
state: {
498497
a: 1
499498
},
500-
getters: makeGetter(1),
499+
getters: {
500+
constant: () => 0,
501+
...makeGetter(1)
502+
},
501503
modules: {
502504
nested: {
503505
state: { a: 2 },

0 commit comments

Comments
 (0)