Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tarang19's avatar

Cannot read property 'getters' of undefined

'getters' of undefined my code

todo.vue

<template>
<div>
<h3>Todos</h3>
<div class="todos">
	<div v-for="todo in allTodos" :key="todo.key" class="todo">
		{{ todo.title }}
	</div>
</div>
</div>
</template>

<script>
	import { mapGetters } from 'vuex'; 
export default {
    name: 'Todos',
    computed: mapGetters(["allTodos"]),
    
  };
</script>

<style>
	.todos {
  border: 1px solid #ccc;
  background: #41b883;
  padding: 1rem;
  border-radius: 5px;
  text-align: center;
  position: relative;
  cursor: pointer;
}
</style>

vuex js

//import axios from 'axios';

const state = {
	todos: [
		{
			id:1,
			title: 'Todo One'
		},
		{
			id:2,
			title: 'Todo two'
		},
	]
};

const getters = {
	allTodos:(state) => state.todos
};

const actions = {};

const mutations = {};

export default {
	state,
	getters,
	actions,
	mutations
};

main.js

import Vue from 'vue';
import App from '~/App.vue';
import Store from '~/store';
import 'bootstrap';

var $ = require("jquery");
window.jQuery = $;

Vue.config.productionTip = false;

new Vue({
	Store,
  render: (h) => h(App),
}).$mount('#app');
0 likes
9 replies
tykus's avatar

Where do you new up the Store? I would expect the following that (given you import directly into the Vue instance from your store file, that you Vue.use(Vuex) and new Vuex.Store(...) in there????

tarang19's avatar

in index.js (store)

import Vuex from 'vuex';
import Vue from 'vue';
import todos from './modules/todos';

Vue.use(Vuex);

export default new Vuex.Store({

	modules: {
		todos
	}

});
tykus's avatar

Okay, so the todos module is exposing getters and these are in the global namespace?

The only other thing that appears differently to my typical implementations is this

computed: mapGetters(["allTodos"]),

Does this work; is it the same end result as:

computed: {
	...mapGetters(["allTodos"]),
}
tarang19's avatar

thanks for replay but error remain same

tykus's avatar

What does the Store look like in your Vue devtools? Is it correctly initialised?

tarang19's avatar

Giving me following output on vue devtool (vuex getters and state) witch is correct

{"todos":{"todos":[{"id":1,"title":"Todo One"},{"id":2,"title":"Todo two"}]}}

tykus's avatar
tykus
Best Answer
Level 104

Is this simply a case of case?

new Vue({
	store: Store,
  render: (h) => h(App),
}).$mount('#app');

But then, the Vuex store would not be properly initialized, right?

1 like

Please or to participate in this conversation.