2017-03-03 3 views
0

vuex doc에 주어진 구문을 시도했습니다.상태, getters 및 vue 구성 요소에서 vuex 모듈의 작업을 얻으려고

store.state.a // -> moduleA의 상태 store.state.b // -> moduleB의 상태

/** 
* First we will load all of this project's JavaScript dependencies which 
* includes Vue and other libraries. It is a great starting point when 
* building robust, powerful web applications using Vue and Laravel. 
*/ 

require('./bootstrap'); 

/** 
* Next, we will create a fresh Vue application instance and attach it to 
* the page. Then, you may begin adding components to this application 
* or customize the JavaScript scaffolding to fit your unique needs. 
*/ 
Vue.component('task-index', require('./components/TaskList.vue')); 
Vue.component('task-show', require('./components/TaskShow.vue')); 
Vue.component('note-index', require('./components/NoteList.vue')); 
Vue.component('note-show', require('./components/NoteShow.vue')); 

const notes = { 
    state: { 
     edit: false, 
     list:[], 
     note: { 
      note : '', 
      id : '' 
     } 
    }, 
    mutations: { 
     SET_EDIT: (state, data) => { 
      state.edit = data 
     }, 
     SET_LIST: (state, data) => { 
      state.list = data 
     }, 
     SET_NOTE: (state, data) => { 
      state.note.id = data.id; 
      state.note.note = data.note; 
     }, 
     SET_EMPTY: (state) => { 
      state.note.note = ''; 
     }  
    }, 
    getters: { 
     noteCount: (state) => state.list.length 
    }, 
    actions : { 
     getNote: ({commit,state}) => { 
      axios.get('/api/note/list') 
      .then((response) => { 
       commit('SET_LIST', response.data); 
       commit('SET_EDIT',false); 
       commit('SET_EMPTY'); 
      }) 
     }, 
    } 
}; 

const tasks = { 
    state: { 
     edit: false, 
     list:[], 
     task: { 
      body : '', 
      id : '' 
     } 
    }, 
    mutations: { 
     SET_EDIT: (state, data) => { 
      state.edit = data 
     }, 
     SET_LIST: (state, data) => { 
      state.list = data 
     }, 
     SET_TASK: (state, data) => { 
      state.task.id = data.id; 
      state.task.body = data.body; 
     }, 
     SET_EMPTY: (state) => { 
      state.task.body = ''; 
     }  
    }, 
    getters: { 
     taskCount: (state) => state.list.length 
    }, 
    actions : { 
     getTask: ({commit,state}) => { 
      axios.get('/api/task/list') 
      .then((response) => { 
       commit('SET_LIST', response.data); 
       commit('SET_EDIT',false); 
       commit('SET_EMPTY'); 
      }) 
     }, 
    } 
}; 

const store = new Vuex.Store({ 
    modules : { 
     task : tasks, 
     note : notes 
    } 
}); 

const app = new Vue({ 
    el: '#app', 
    store 
}); 

TaskList.vue

app.js
<template> 
    <div > 
     <h4>{{count}} Task(s)</h4> 
     <ul class="list-group"> 
      <li class="list-group-item" v-for="item in list"> 
       {{item.body}} 
       <button class="btn btn-primary btn-xs" @click="showTask(item.id)">Edit</button> 
       <button class="btn btn-danger btn-xs" @click="deleteTask(item.id)">Delete</button> 
      </li> 
     </ul> 
    </div> 
</template> 
<script> 
export default{ 
    computed :{ 
     list() { 
      return this.$store.state.task.list; 
     }, 
     count(){ 
      return this.$store.getters.taskCount;    
     } 
    }, 
    mounted(){ 
     this.$store.dispatch('getTask'); 
    }, 
    methods : { 

     showTask: function(id){ 
      axios.get('/api/task/'+ id) 
      .then(response => { 
       this.$store.commit('SET_TASK',response.data); 
       this.$store.commit('SET_EDIT',true); 
      }); 
     }, 
     deleteTask: function(id){ 
      axios.delete('/api/task/delete/' + id) 
      this.$store.dispatch('getTask'); 
     } 
    } 
} 
</script> 

'Uncaught TypeError :이 속성에서'undefined 속성 '을 읽을 수 없습니다. 코드 줄 '반환이. $ store.state.task.list;'

답변

2

그래서 당신은 단지 vuex 루트 컨텍스트에서 게터를 사용할 수 있습니다.

0

글쎄, 당신이 당신의 국가의 구조와 일치하지 않는 검색하려는 상태 : 당신이 this.$store.state.task.listthis.$store.state.list에 변경하는 경우

state: { 
    edit: false, 
    list:[], 
    note: { 
     note : '', 
     id : '' 
    } 
}, 

그러면 당신은 모든 걸 포기하고 패치해야합니다. vuex의 documentation

By default, actions, mutations and getters inside modules are still registered under the global namespace

에 acoording

+1

작업은 thal 목록이 속한 모듈입니다. 몇 가지 수정 사항이 이제는 이것을 사용하여 상태를 가져올 수 있습니다. $ store.state.task.list.But 내가 gettters에 대해 동일한 구문을 사용할 때, '이것을 반환하십시오. . $ store.getters.task.taskCount; ' 표시되는 오류는 "Uncaught TypeError : undefined"의 taskCount '속성을 읽을 수 없습니다. 그러나'return this. $ store.getters.taskCount; '와 같이 코드를 작성하면 모든 것이 정상입니다. – Shalom

+0

음, 기본적으로 위에서 설명한 것과 동일한 것을 보여줍니다. 상점을 구조화하는 방법과 실제로 구조화 된 방법 사이에는 단절이있는 것 같습니다. Chrome 용 Vue.js Devtools 플러그인을 사용하고 있습니까? 그러면 이러한 유형의 문제를 쉽게 해결할 수 있습니다. – K3TH3R

+0

그래, Vue.js Devtools를 사용하고있다 – Shalom

관련 문제