ej001
ej001

Reputation: 9

How to save initial state value in a variable vuejs

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

Answers (1)

Igor Moraru
Igor Moraru

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

Related Questions