question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add a new property to vue (object) data?

See original GitHub issue

My data goes below

 data: {
                contacts: {
                    "1": {
                        isSelected: false,
                        UID:"1",
                        cardType: 0      
                    },
                    "2": {
                        isSelected: false,
                        UID:"2",
                        cardType: 0    
                    },
                    "3": {
                        isSelected: false,
                        UID:"3",
                        cardType: 0    
                     },
            }
...

I could use this method to add new contact, but it got a compatibility problem, so I wonder are there some other ways to add new property to object, or I’d better change to use array instead of object. this.contacts = Object.assign({}, this.contacts, newObj);

Thanks a lot.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

41reactions
wufeng87commented, Jul 28, 2016

Thanks, using the set method, and It’s OK now.

this.$set('contacts[' + newPsgId + ']', newObj)
Vue.set(this.contacts[newPsgId], 'name', this.editPsgName);  
28reactions
fnlctrlcommented, Jul 28, 2016

Read the docs: Vue.set

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add properties to a object in vue - Stack Overflow
Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. – jeremy.Wu.
Read more >
Using Vue.js $set() method to set data object properties
We can use the $set() method in a v-on:click directive to set a property to a value when the element is clicked. This...
Read more >
How do I add a new object into an existing object and retain ...
The following inserts a new object into an already existing one. Vue.set(this.cards[x].group[y] = Object.assign(this.cards[x].group[y], ...
Read more >
How to dynamically create reactive properties in Vue
Vue does not allow adding new root-level properties to the data object after the initialization of a component. This is because Vue modifies ......
Read more >
How to properly add props to data object in Vue.js - Medium
It turned out that I couldn't add the property directly to the data object. Instead, I should use Object.assign() . Well, a code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found