Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 305
Kakar Avatar asked Feb 25 '17 20:02

Kakar


2 Answers

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';

like image 52
Traxo Avatar answered Oct 06 '22 07:10

Traxo


//state.js

export const store = new Vuex.Store({

    state :{
         title :"Welcome in Vuex Tutorials By SK Islam"
    }

});

Then in main.js please import the store in this way..

import {store} from "./store/state";

{store}// this is important..

like image 38
SK AMINUL ISLAM Avatar answered Oct 06 '22 08:10

SK AMINUL ISLAM