1๐
// residents.js
import Vue from 'vue'
const state = {
residents: {}
}
const mutations = {
RESIDENT_ADD (state, value) {
Vue.set(state.residents, value.residentId, value)
}
}
const actions = {
}
const getters = {
getResidents: state => {
const arr = []
for (const key in state.residents) {
const obj = state.residents[key]
arr.push(obj)
}
return arr
},
getResidentById: state => id => {
const obj = state.residents[id] ? state.residents[id] : null
return obj
}
}
export default {
namespaced: true,
state,
mutations,
actions,
getters
}
this should do the trick.
btw. avoid using arrays in state since it triggers a lot of getters if you remove or add an item to the array.
if you need an array on the view make a specific getter for it.
๐คJohnny
Source:stackexchange.com