2017-02-25 171 views
1

我想以這種方式來使用Vuex ("^2.1.3")vuejs ("^2.1.10")項目:Vuex商店狀態不定

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

一些組分.js:

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

我現在有兩個問題:

  1. 首先是我在我的控制檯得到一個警告

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

  • 另一個問題是,state沒有定義。
  • Uncaught TypeError: Cannot read property 'state' of undefined

    我很新的這一點,可能是我錯過了一些東西,所以請原諒我。我錯過了什麼?

    +1

    嘗試'出口默認新Vuex.Store({',而不是'出口常量店=新Vuex.Store({''你的store.js中可能是?' – Traxo

    +1

    或'從'./store';'0123'進口{store};' – dfsq

    +0

    是的!我嘗試了兩種方式,都是解決問題的答案。 – Kakar

    回答

    6

    在你store.js:

    export default new Vuex.Store({,而不是export const store = new Vuex.Store({

    或者作爲@dfsq說import {store} from './store';