0

I know that the default behaviour of a object when we create new atributes for the same instance is that it reference the old, changing the properties.

I have something like this on my vue data:

export default {
    data() {
        return {
            paragraph: {
                text: "",
                fontSize: 14,
                key: "Paragraph",
                align: "left"
            }

        }
    },
    methods: {
        addParagraph() {
            this.$set(this.paragraph, 'key', this.paragraph.key);
            this.$set(this.paragraph, 'text', this.paragraph.text);
            this.$set(this.paragraph, 'fontSize', this.paragraph.fontSize);
            this.$set(this.paragraph, 'align', this.paragraph.align);
            this.$store.commit("appendToDocument", this.paragraph)
        },
        alignment(option) {
            this.paragraph.align = option;
        }
    }

everytime i click a button the data inside the paragraph changes and i want to pas the data to vuex store to add it to a json, so i can have a tree of paragraphs, the problem is, that everttime i create a new paragrapg it changes the values of my other paragraphs created before, is there a way to change it?

2 Answers 2

3

@Potray answer is good. But it can be even shorter if you are using Babel with stage-3 (spread operator). Then you can copy all properties with that syntax

addParagraph() {
    this.$store.commit("appendToDocument", { ...this.paragraph })
},
Sign up to request clarification or add additional context in comments.

7 Comments

what does the ... mean?
here you find nice examples github.com/tc39/proposal-object-rest-spread/blob/master/… 3 dots operator in that case means: create new object A then copy all properties from object B and then paste add them to abject A. Basically in your case { ...this.paragraph } do exactly what @potray code. You may be familiar with spread/rest operator for arrays. it landed in ES2015. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Spread/rest for objects is almost a standard
just a think, what happens to the reference, id doesn't care? i like yout solution im curious :D
value types are copied ... so there is no reference between source object and target object
but if your property in source object is an object then target and source point to same place
|
2

Try this:

 addParagraph() {
        var paragraph = {
           key: this.paragraph.key,
           text: this.paragraph.text,
           fontSize: this.paragraph.fontSize,
           align: this.paragraph.alignkey,
        }
        this.$store.commit("appendToDocument", paragraph)
    },

1 Comment

not my favourite solution since i need to write all again, but it works :D, thank you !!

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.