Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

87
Views
¿Cómo esperar a que se complete la acción antes de acceder al estado de Vue Store?

Tengo una aplicación Vuejs/Nuxtjs dentro de la cual necesito acceder al state de una tienda Vuex después de que haya sido modificada por Vuex action . Actualmente, cuando intento ejecutar la action y la assignment , obtengo el estado anterior y no el que se actualizó después de action .

¿Cómo hacer que el código espere a que se complete la action y luego ejecute la siguiente declaración? El siguiente es el código que tengo actualmente: Componente Vuejs:

 <template> <div> <input v-model="formData.value" type="text"> <button @click="performAction"> Click Me </button> </div> </template> <script> export default { data () { return { formData: { value: '', returnValue: '' } } }, methods: { performAction () { // Set the value within the Vuex Store this.$store.commit('modules/DataStore/populateData', this.formData.value) // Perform the Action this.$store.dispatch('modules/DataStore/getData').then(() => { console.log("AFTER COMPLETE ACTION") }) // Assign the update value to the variable this.formData.returnValue = this.$store.state.modules.DataStore.data } } } </script> <style> </style>

Tienda Vuex:

 export const state = () => ({ data:'' }) export const mutations = { populateData (state, data) { state.data = data } } export const actions = { getData ({ commit, state, dispatch }) { const headers = { 'Content-Type': 'application/json' } this.$axios .post('/getUrlData', state.data, { headers }) .then((response) => { console.log("WITHIN RESPONSE") commit('populateData',response.data) }) .catch((error) => { commit('populateData', 'Unable to obtain data, Error : ' + error) }) } }

Lo siguiente es lo que probé y nada funciona en este momento:

  1. Probé la función .then() .
  2. Intenté Async y await , pero ambos no funcionan.

Cualquier sugerencia será realmente apreciada. Gracias por adelantado.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puedes crear getter en vuex :

 export const getters = { getData: (state) => state.data, }; export const actions = { async setData ({ commit }, data) { const headers = { 'Content-Type': 'application/json' } await this.$axios .post('/getUrlData', data, { headers }) .then((response) => { console.log("WITHIN RESPONSE") commit('populateData',response.data) }) .catch((error) => { commit('populateData', 'Unable to obtain data, Error : ' + error) }) } }

luego, en el componente, puede asignar captadores y acciones, y llamarlos:

 import { mapGetters, mapActions } from 'vuex' computed: { ...mapGetters(['getData']), }, methods: { ...mapActions(['performAction']), async performAction() { await this.setData(this.formData.value) this.formData.returnValue = this.getData } }
about 4 years ago · Juan Pablo Isaza Report

0

Debe devolver su promesa en su si desea encadenarla en el método de llamada. p.ej:

 getData ({ commit, state, dispatch }) { const headers = { 'Content-Type': 'application/json' } return this.$axios // now this promise will be returned and you can chain your methods together .post('/getUrlData', state.data, { headers }) .then((response) => { console.log("WITHIN RESPONSE") commit('populateData',response.data); return response.data; //this will allow you do send the data through to the next Then() call if you want to }) .catch((error) => { commit('populateData', 'Unable to obtain data, Error : ' + error) }) }

Esta situación es mucho más fácil de manejar con async-await IMO. Se vuelve:

 export const actions = { async getData ({ commit, state, dispatch }) { const headers = { 'Content-Type': 'application/json' } const response = await this.$axios.post('/getUrlData', state.data, { headers }); console.log("WITHIN RESPONSE") commit('populateData',response.data); } }

y

 methods: { async performAction () { // Set the value within the Vuex Store this.$store.commit('modules/DataStore/populateData', this.formData.value) // Perform the Action await this.$store.dispatch('modules/DataStore/getData'); console.log("AFTER COMPLETE ACTION"); // Assign the update value to the variable this.formData.returnValue = this.$store.state.modules.DataStore.data } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!