Search code examples
vuejs2vue.jsvuex

Vuex store state is undefined


I am trying to use Vuex ("^2.1.3") with vuejs ("^2.1.10") project in this way:

store.js:

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

export const store = new Vuex.Store({
    state: {
        inTheaters: [
            {
                name: 'Resident Evil: The Final Chapter',
                poster_url: 'https://blackgirlnerds.com/wp-content/uploads/2017/02/Resident-Evil-The-Final-Chapter-Final-Poster-Featured.jpg',
                language: 'English',
            },
            {
                name: 'Irada',
                poster_url: 'http://filmywave.com/wp-content/uploads/2017/02/irada-movie-poster-1.jpg',
                language: 'Hindi',
            },
        ]
    },
});

main.js:

import store from './store';

new Vue({
    router,
    components: {App},
    template: '<App/>',
    store,
}).$mount('#app');

some-component.js:

<script>
    export default {
        name: 'movieListWrapper',
        props: {
            movieListType: {
                type: String,
                default: 'in-theateras',
            },
        },
        computed: {
            movieList() {
                return this.$store.state.inTheaters;
            }
        },
    }
</script>

I now have two problems:

  1. First is I get a warning in my console

"export 'default' (imported as 'store') was not found in './store'

  1. The other problem is that the state is not defined.

Uncaught TypeError: Cannot read property 'state' of undefined

I am very new to this and may be I am missing some thing so please pardon me. What am I missing?


Solution

  • In your store.js:

    export default new Vuex.Store({ instead of export const store = new Vuex.Store({

    Or as @dfsq said import {store} from './store';