Vuex

2016-08-27 14 views
5

में कार्रवाई के अंदर कार्रवाई को कैसे कॉल करें I Rails API के साथ वास्तव में एक छोटा Vue ऐप बनाने का प्रयास करें। तो फिलहाल मैं वू, वू-रिसोर्स और वूएक्स के साथ काम करता हूं। मैं डेटाबेस से सभी उपयोगकर्ताओं को वापस लाऊंगा, अब मैं उनमें से एक को अपडेट करने का प्रयास करता हूं। तो सब कुछ ठीक काम करता है (पैच उपयोगकर्ता), लेकिन अद्यतन यूज़र कार्रवाई चलाने के बाद मैं Vuex स्टोर को अद्यतन करने के लिए fetchUsers कार्रवाई को फिर से चलाने के लिए चाहता हूं।Vuex

लेकिन fetchUsers updateUser वादा मैं निम्नलिखित त्रुटि मिलती है अंदर चलता है जब: मैं अब कि fetchUsers

export const fetchUsers = function ({ dispatch, state }) { 
    this.$http.get('http://localhost:3000/api/v1/users').then((response) => { 
    dispatch('SET_USERS', response.data) 
    }, (response) => { 
    dispatch('SET_USERS', []) 
    console.log(response) 
    }) 
} 

export const updateUser = function ({ dispatch, state }, user) { 
    this.$http.patch('http://localhost:3000/api/v1/users/' + user.id, {user: user}).then((response) => { 
    fetchUsers() 
    }, (response) => { 
    console.log(response) 
    }) 
} 

:

undefined:1 Uncaught (in promise) TypeError: Cannot read property 'dispatch' of undefined 

यह है कि क्या मेरी vuex/actions.js देख रहे है कार्रवाई किसी भी तरह से संदर्भ खो देता है (?) लेकिन मुझे नहीं पता कि इससे कैसे निपटें! हर मदद के लिए धन्यवाद!

उत्तर

9

यह कैसे मैं इसे :)

import Vue from 'vue' 

export const fetchUsers = ({ dispatch, state }) => { 
    Vue.http.get('http://localhost:3000/api/v1/users').then((response) => { 
    dispatch('SET_USERS', response.data) 
    }, (response) => { 
    dispatch('SET_USERS', []) 
    console.log(response) 
    }) 
} 

export const updateUser = ({ dispatch, state }, user) => { 
    Vue.http.patch('http://localhost:3000/api/v1/users/' + user.id, {user: user}).then((response) => { 
    fetchUsers({dispatch}) 
    }, (response) => { 
    dispatch('SET_USERS', []) 
    console.log(response) 
    }) 
} 
+0

मैं संरचना समान है काम करने के लिए मिल गया है, im हो रही fetchUsers परिभाषित नहीं है। –

संबंधित मुद्दे