javascript - Load state with async action Vuex - Stack Overflow

I can't realize how to get it works.I'm trying to load a propertie (productos) on my data() w

I can't realize how to get it works.

I'm trying to load a propertie (productos) on my data() which has to catch the value from a state.

My ponent:

    data () {
        return {
          productos: this.$store.state.mStock.productos
        }
     },
  created() {
    this.$store.dispatch('fetchProductos')
  }

At this point i think it's okay, when i load my ponent i dispatch my action to load the state on the store. I think the problem is that the way i fill the state is ASYNC

Store:

import StockService from '@/services/StockService'

export const moduleStock = {
  strict: false,
  state: {
    productos: []
  },
  mutations: {
    setProductos (state, payload) {
      state.productos = payload.productos
    }
  },
  actions: {
    async fetchProductos ({mit}, payload) {
      const resp = await (StockService.getProductos())
      var productos = resp.data
      mit('setProductos', {productos: productos})
    }
  }
}

When i load my ponent, the propertie "productos" on the data() is null, however if i see the 'state.productos' from the Vuex devtools, it has the data!

I'm messed up.

I can't realize how to get it works.

I'm trying to load a propertie (productos) on my data() which has to catch the value from a state.

My ponent:

    data () {
        return {
          productos: this.$store.state.mStock.productos
        }
     },
  created() {
    this.$store.dispatch('fetchProductos')
  }

At this point i think it's okay, when i load my ponent i dispatch my action to load the state on the store. I think the problem is that the way i fill the state is ASYNC

Store:

import StockService from '@/services/StockService'

export const moduleStock = {
  strict: false,
  state: {
    productos: []
  },
  mutations: {
    setProductos (state, payload) {
      state.productos = payload.productos
    }
  },
  actions: {
    async fetchProductos ({mit}, payload) {
      const resp = await (StockService.getProductos())
      var productos = resp.data
      mit('setProductos', {productos: productos})
    }
  }
}

When i load my ponent, the propertie "productos" on the data() is null, however if i see the 'state.productos' from the Vuex devtools, it has the data!

I'm messed up.

Share Improve this question asked Oct 5, 2018 at 14:27 AtarC5AtarC5 4132 gold badges12 silver badges30 bronze badges 1
  • Use productos as a puted property, not in data(). – Bennett Dams Commented Oct 5, 2018 at 14:32
Add a ment  | 

1 Answer 1

Reset to default 5

The data() method is only run once.

This might seem to work if when the ponent and the vue store use the same object instance, but doesn't work in this case because a new array instance is assigned in the store while the ponent stil has the previous instance (the empty array)

Use puted properties. I remend using the mapState() helper:

puted: mapState({
  productos: state => state.mStock.productos
})

without mapState you'd write:

puted: {
  productos() {
    return this.$store.state.mStock.productos
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745507150a4630602.html

相关推荐

  • javascript - Load state with async action Vuex - Stack Overflow

    I can't realize how to get it works.I'm trying to load a propertie (productos) on my data() w

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信