0

Hello i have a function that recieves a percentage of download of a file, and i use vue set to add the property to the object cuz it's not reactive yet, now this function gets called every second to update the percentage and i'm wondering if it's ok to always use vue set to both set it and update the value, or after i used vue set check if the property it's there and then update it regularly

So i can always call this like this

SettaPerc(fileIndex = -1, profIndex = -1,perc = 0) {
            if (profIndex === -1) {
                this.$set(this.file[fileIndex], "perc", perc);
            } else {
                this.$set(this.file[profIndex].linkondi[fileIndex], "perc", perc);
            }
        },

Or it's better to do this

        SettaPerc(fileIndex = -1, profIndex = -1, perc = 0) {
        if (profIndex === -1) {
            if (this.file[fileIndex].perc === undefined) {
                this.$set(this.file[fileIndex], "perc", perc);
            } else {
                this.file[fileIndex].perc = perc
            }
        } else {
            if (this.file[profIndex].linkondi[fileIndex] === undefined) {
                this.$set(this.file[profIndex].linkondi[fileIndex], "perc", perc);
            } else {
                this.file[profIndex].linkondi[fileIndex] = perc
            }
        }
    },

1 Answer 1

1

There's no real downside to use this.$set, it's completely fine.

My suggestion would be

  SettaPerc(fileIndex = -1, profIndex = -1,perc = 0) {
      if (profIndex === -1) 
        return this.$set(this.file[fileIndex], "perc", perc); 

      return this.$set(this.file[profIndex].linkondi[fileIndex], "perc", perc);           
  },

So you could get rid of the additional else block

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.