Reputation: 9
I have an object with some data in the vuex store that I will need to use in a component,I have accessed the data with a getter in the component. I am trying to save the initial value of that object in a variable in a way that it doesn't change/update when the data in the store changes, this will allow me to do a comparison condition.
the code is below, the object accessed by 'getSelectedLocation' is what I need the original value of kept.
data() {
return {
toggle: this.getSelectedLocation?.local,
}
},
computed: {
...mapGetters({ getUser: 'user/getUser', getSelectedLocation: 'support/getSelectedLocation' }),
},
methods: {
async updateUsersLocation() {
const usersCol = new UsersCollection(this.$fire.firestore)
const id = this.getUser.id
const location = this.getSelectedLocation
return await usersCol.updateUsersLocation({ id, location })
},
},
}
Upvotes: 0
Views: 1393
Reputation: 7729
You need two things. First to make a copy of that object, so it cannot change by reference the initial object, then to freeze it, so it cannot change itself.
const copy = Object.assign({}, this.getSelectedLocation())
Object.freeze(copy)
Upvotes: 1