Vue2 + Vuex提交未提交(没有Vue devtools)

问题描述 投票:1回答:1

我正在努力完成Vuex的基本任务,但由于某种原因它不起作用,经过广泛的搜索,我非常感谢任何帮助。

我正在尝试做什么:

使用新属性(对象)更新商店中对象的列表(对象)。

出了什么问题:

在我甚至调度操作以从我的组件提交新对象之前(我通过mapActions访问操作),列表中任何现有对象中的某些属性将使用与组件中的输入/ v模型关联的值进行更新。正如我的代码如下所示,我知道对象的反应性是一个问题,所以我按照文档使用Vue.set(...)Mutations Follow Vue's Reactivity Rules

为什么我不认为我做的事情完全是愚蠢的...(但可能是错的)

当我检查DevTools中的变异时,会按预期记录变异,当我按下“Commit”/“Commit All”时,列表中的现有对象不再响应输入中的变化。这显然是我期望发生的行为,因为该行动实际上应该将更改提交给状态。然而,为什么它不能在代码中工作,只能在devtools中工作?

我再次为可能是一个基本问题道歉,但我看到其他一些人有类似的问题,没有书面解释我们错过了什么...

初始状态

const state = {
  quotes: {}
}

突变

mutations: {
  [types.ADD_QUOTE] (state, payload) {
    Vue.set(state.quotes, payload.id, payload)
  }
}

行动

actions: {
  addQuote ({ commit }, payload) {
    commit(types.ADD_QUOTE, payload) 
  }    
}

零件

<template>
  <div class="quote-block">
    <label>price</label>
    <input type="text" v-model="quote.price">
    <label>id</label>
    <input type="text" v-model="quote.id">
    <!-- Just displaying props below -->
    <div>{{ quote.item }}</div>
    <div>{{ quote.vendor }}</div>
    <div>Qty: {{ quote.qty }}</div>
    <button @click="addQuote(quote)">Submit quote</button>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  props: {
    vendor: String,
    item: String,
    qty: Number
  },
  data () {
    return {
      quote: {
       id: '',
       price: '',
       timestamp: Date.now(),
       vendor: this.vendor,
       item: this.item,
       qty: this.qty
      }
     }
    },
  methods: {
    ...mapActions([
      'addQuote'
    ])
   }
  }

总而言之,在devtools中,我看到idprice的值在我设置为state.quotes的对象中发生变化 - 它们显然与我的组件中的quote.pricequote.id的v模型相关联。只有当我在devtools中“全部提交”时,这些对象的属性才会停止变化。为什么在进行这些提交的动作中没有commit方法?

vuejs2 vuex vue-devtools
1个回答
2
投票

你陷入了对象参考陷阱。引用是一个对象,它作为有效负载传递给动作。当您提交该有效负载时,它会将引用保存到您的状态。

现在组件和存储都指向同一个对象。

解决方案是使用spread运算符或Object.assign将输入复制到新对象中

通常一个好的做法是始终在mutator中复制有效负载(如果它是一个对象)

function(state, payload){
  Vue.set(state.quotes, payload.id, {... payload });
}
© www.soinside.com 2019 - 2024. All rights reserved.