我有一个组件,Villager
它prop
从父组件接收到一个数组。父组件从中拉出该数组this.$store.state
。
return this.$store.state.villages.find(value => value.id === 0).villagers
之后我执行一次this.$store.mutation
提交以更改给定的数组。如屏幕截图所示,该突变确实有效。但是,我所在的组件没有重新渲染,仍然显示3而不是2。
反应链似乎在某个地方坏了,但我无法确定这个魔鬼。我认为props
价值是一种反应机制。如您在屏幕快照中所见,它的值已更改,但DOM并未相应更新。
我尝试提取问题的相关部分
商店/index.ts
[...]
export default new Vuex.Store({
state: {
villages: [
{
id: 0,
name: 'Peniuli',
foundingDate: 20,
villagers: [
{
id: '33b07765-0ec6-4600-aeb1-43187c362c5a1',
name: 'Baltasar',
bloodline: 'Gent',
occupation: 'Farmer'
},
[...]
mutations: {
disbandVillager (state, payload) {
const villagerId = payload.id
const village = state.villages.find(value => value.id === 0)
console.debug('disbanding villager:', villagerId)
if (!_.isNil(village)) {
console.debug('bfore:', village.villagers)
_.remove(village.villagers, function (n) {
return n.id === villagerId
})
console.debug('after:', village.villagers)
}
}
},
[...]
村景
<template>
<Villagers :villagers="playerVillage.villagers"></Villagers>
</template>
[...]
computed: {
playerVillage: function () {
return this.$store.state.villages.find(value => value.id === 0)
}
}
[...]
村民
<template>
<v-container>
<v-card v-for="villager in villagers" :key="villager.id">
<v-row>
<v-col>
<v-card-title>Name: {{villager.name}}</v-card-title>
</v-col>
<v-col>
<v-card-title>Bloodline: {{villager.bloodline}}</v-card-title>
</v-col>
<v-col>
<v-card-title>Occupation: {{villager.occupation}}</v-card-title>
</v-col>
<v-col v-if="managable">
<DisbandButton :villager="villager"></DisbandButton>
</v-col>
</v-row>
</v-card>
</v-container>
</template>
<script>
import DisbandButton from '@/components/village/DisbandButton'
export default {
name: 'Villagers',
components: { DisbandButton },
props: [
'villagers',
'managable'
]
}
</script>
DisbandButton.vue
<template>
<v-btn @click="disbandVillager" color="red">Disband</v-btn>
</template>
<script>
export default {
name: 'DisbandButton',
props: ['villager'],
methods: {
disbandVillager: function () {
this.$store.commit('disbandVillager', { id: this.villager.id })
}
}
}
</script>
假设那是lodash,那么我相信问题是您正在使用_.remove
。
在内部_.remove
以不触发Vue的反应系统的方式实现。具体在这里:
https://github.com/lodash/lodash/blob/ded9bc66583ed0b4e3b7dc906206d40757b4a90a/lodash.js#L3859
那是使用默认的Array splice
方法,而不是Vue提供的自定义替代。
这样的事情应该起作用:
village.villagers = village.villagers.filter(function (n) {
return n.id !== villagerId
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句