1import { mapGetters } from 'vuex'
2
3export default {
4 // ...
5 computed: {
6 // mix the getters into computed with object spread operator
7 ...mapGetters([
8 'doneTodosCount',
9 'anotherGetter',
10 // ...
11 ])
12 }
13}
14
1const store = new Vuex.Store({
2 state: {
3 todos: [
4 { id: 1, text: '...', done: true },
5 { id: 2, text: '...', done: false }
6 ]
7 },
8 getters: {
9 doneTodos: state => {
10 return state.todos.filter(todo => todo.done)
11 }
12 }
13});
14
15console.log(store.state.getters.doneTodos);
16// -> [{ id: 1, text: '...', done: true }]
1computed: {
2 ...mapState({
3 a: state => state.some.nested.module.a,
4 b: state => state.some.nested.module.b
5 }),
6 ...mapGetters([
7 'some/nested/module/someGetter', // -> this.$store.getters['some/nested/module/someGetter']
8 'some/nested/module/someOtherGetter', // -> this.$store.getters['some/nested/module/someOtherGetter']
9 ])
10},
11methods: {
12 ...mapActions([
13 'some/nested/module/foo', // -> this.$store.getters['some/nested/module/foo']()
14 'some/nested/module/bar' // -> this.$store.getters['some/nested/module/bar']()
15 ])
16}
17
1computed: {
2 ...mapState('some/nested/module', {
3 a: state => state.a,
4 b: state => state.b
5 }),
6 ...mapGetters('some/nested/module', [
7 'someGetter', // -> this.someGetter
8 'someOtherGetter', // -> this.someOtherGetter
9 ])
10},
11methods: {
12 ...mapActions('some/nested/module', [
13 'foo', // -> this.foo()
14 'bar' // -> this.bar()
15 ])
16}
17