【发布时间】:2021-03-09 07:02:34
【问题描述】:
我有一个列出来自数据库的维护请求的 Vue 组件。
<template>
<div>
{{ maintenance_requests }}
</div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
export default {
computed: {
...mapGetters({
maintenance_requests: 'maintenance/maintenances',
}),
},
methods: {
...mapActions({
getMaintenanceRequests: 'maintenance/getMaintenanceRequests',
}),
},
mounted () {
this.getMaintenanceRequests()
}
}
</script>
这是我的 Vuex 商店
import axios from 'axios'
export default {
namespaced: true,
state:{
maintenance_requests: [],
},
getters:{
maintenances (state) {
return state.maintenance_requests.sort((a,b) => b.date_created - a.date_created)
},
mutations:{
PUSH_MAINTENANCES (state, data) {
state.maintenance_requests.push(...data)
},
actions:{
async getMaintenanceRequests ({ commit }) {
let response = await axios.get('/api/maintenance/requests')
commit('PUSH_MAINTENANCES', response.data.data)
}
}
},
以上代码输出维护列表如下:
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
我也在用 Vue 路由器
import AppMaintenanceForm from './components/maintenance/AppMaintenanceForm.vue'
import AppListMaintenanceRequests from './components/maintenance/AppListMaintenanceRequests.vue'
export const routes = [
{
path: 'list/maintenance/requests',
component: AppListMaintenanceRequests,
name: 'ListMaintenanceRequests'
},
{
path: '/maintenance/form',
component: AppMaintenanceForm,
name: 'MaintenanceForm'
},
]
问题是,每次我切换路线时。例如,从维护表格到维护请求列表,我得到了维护请求的副本。
而不是得到这个:
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
我明白了:
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
并且对于路由的每次后续切换,它都会继续复制。
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
{"maintenance_id": 1, "type": "Water problems", "Description": "I have had no water for 2 days now" },
{"maintenance_id": 2, "type": "Electricity problems", "Description": "My bulb is faulty" },
{"maintenance_id": 3, "type": "Roof problems", "Description": "My roof, in the guest bedroom is leaking" },
如何消除这种重复?谢谢。
【问题讨论】:
标签: javascript json vue.js vuex vue-router