简体   繁体   English

如何在 vuex 中响应式管理 state?

[英]How can I manage state in vuex reactively?

I am trying to solve a problem in my vuex store.我正在尝试解决我的 vuex 商店中的问题。 I write two different actions in my store.我在商店里写了两个不同的动作。 One action is reactive and the other not.一个动作是反应性的,另一个不是。 But I need the loadSlidesEach() in reactivity, so the data are updated.但我需要loadSlidesEach()的反应性,所以数据被更新。 I cant find the mistake.我找不到错误。

My store:我的店铺:

const store = new Vuex.Store({

  state: {
    loadedSlides: []
  },

  mutations: {
    setSlides(state, slides) {
      state.loadedSlides = slides
    },
    setSlidesPush(state, slide) {
      state.loadedSlides.push(slide)
    }
  },

  getters: {
    loadedSlides(state) {
      return state.loadedSlides
    }
  },

  actions: {
    loadSlides({ commit, getters, state }) {
      firebase.database().ref('/slides/').on('value', snapshot => {
        const slidesArray = []
        const obj = snapshot.val()
        for (const key in obj) {
          slidesArray.push({ ...obj[key], id: key })
        }
        commit('setSlides', slidesArray)
      })
    },
    loadSlidesEach({ commit, getters, state }, id) {
      firebase.database().ref('/slides/' + id).on('value', snapshot => {
        const slide = snapshot.val()
        commit('setSlidesPush', { ...slide })
      })
    }
  }
})

My component 1: Array from slides() is reactive我的组件 1:slides() 中的数组是反应式的

export default {
  computed: {
    slides() {
      return this.$store.getters.loadedSlides
    }
  },
  created() {
    this.$store.dispatch('loadSlides')
  }
}

My component 2: Array from slides() is not reactive我的组件 2:slides() 中的数组不是反应式的

export default {
  computed: {
    slides() {
      return this.$store.getters.loadedSlides
    }
  },
  created() {
    const slides = ['1','2','3']
    for (let i = 0; i < slides.length; i++) {
      this.$store.dispatch('loadSlidesEach', slides[i])
    }
  }
}

I think the problem is something with the inital state or the mutation with push().我认为问题出在初始 state 或 push() 的突变上。 Any advices?有什么建议吗?

Update: The two actions are only different in the mutations.更新:这两个动作只是突变不同。 So what is the best way to set the state in vuex?那么在 vuex 中设置 state 的最佳方法是什么? The store get confused if I call the action loadSlidesEach() in a loop.如果我在循环中调用操作 loadSlidesEach(),商店会感到困惑。

Don't use await and then together.不要将awaitthen一起使用。 Use one or another:使用一个或另一个:

loadSlidesEach: async({ commit, getters }, id) => {
  const data = await firebase.database().ref('/slides/' + id).once('value')
  const slide = data.val()
  commit('setSlidesPush', { ...slide })
}

do you try to use mapState from vuex ?:您是否尝试使用mapState中的vuex

import {mapState} from 'vuex'

export default {
  computed: {
     ...mapState(['loadedSlides '])
  }
}

now you can use loadedSlides in component.现在您可以在组件中使用loadedSlides。

I found a working solution for my problem.我为我的问题找到了一个可行的解决方案。

Change the mutation:改变突变:

setSlidesPush(state, addedSlide) {
  const slideIndex = state.loadedSlides.findIndex(slide => slide.id === addedSlide.id)
  if (slideIndex !== -1) {
    Vue.set(state.loadedSlides, slideIndex, addedSlide)
  } else {
    state.loadedSlides.push(addedSlide)
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从存储值响应式更新组件中的值? - How can I reactively update a value in a component from a store value? 如何在 ReactJS 的功能组件中管理 4 Checkbox 的状态? - How I can manage the state for 4 Checkbox in functional component in ReactJS? 如何从 axios 响应对象中的所有项目中提取特定值到 aa vuex 状态 - How can I extract specific values from all items in axios response object to a a vuex state 离开域并返回后,如何保存我的 VueX state? - How can I save my VueX state after leaving the domain and returning? vue3 composition api和vuex一起使用时,怎么把state变的function分开? - When using Vue3 composition api and vuex together, how can I separate the function that changes state? 在$ state.href上打开新窗口时,如何管理依赖于$ state角度变化的元素? - How can I manage elements dependent on angular $state change when opening a new window on $state.href? 我可以在 Vuex 的 settimeout function 内更改并提交 state 吗? - Can I change and commit the state inside settimeout function in Vuex? 如何从作为道具传递的数组中反应性地删除一个对象,以便它反映在 DOM 中? - How can I reactively remove an object from an array which is passed as a prop so that it is reflected in the DOM? 我无法改变 vuex 的状态 - I could not change state of vuex 如何将 Vuex 集成到 Storyblok 插件中? - How can I integrate Vuex into a Storyblok plugin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM