H
H
HaiZenBerGG2022-02-18 20:31:33
Vue.js
HaiZenBerGG, 2022-02-18 20:31:33

Vue calculate and write a new property to the object (this.$set is not a function)?

Hello, the question is the following: there is an array of objects that needs to be drawn as a list,
but before drawing, you need to calculate a new property and enter it into the object
, I do it like this

computed: {
    calculatedTables:function () {
      for (let table in this.tables) {
        this.$set(table, 'datetime',555)
        console.log(table)
      }
}

I get an error in the browser console
(in promise) TypeError: this.$set is not a function

when inserting through Object.assign all the rules except that the property is not rendered
what to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ice, 2022-02-18
@IceRD

computed - must only perform a calculation and return a result, it must not mutate the data.
Use methods or watch to change data

I
InfernoElegy, 2022-02-26
@InfernoElegy

You can't mutate in computed
If you need to add an object field, you can use .map

return this.tables.map(table => ({ ...table, datetime: 555 }))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question