Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wufeng87 opened this issue Jul 28, 2016 · 5 comments
Closed

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

wufeng87 opened this issue Jul 28, 2016 · 5 comments

Comments

@wufeng87
Copy link

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.

@fnlctrl
Copy link
Member

fnlctrl commented Jul 28, 2016

Read the docs: Vue.set

@wufeng87
Copy link
Author

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

this.$set('contacts[' + newPsgId + ']', newObj)
Vue.set(this.contacts[newPsgId], 'name', this.editPsgName);  

@CHNB128
Copy link

CHNB128 commented Sep 8, 2019

The previous solution does not work for me (Vue ^ 3.0.0)
Do it myself. Hope this helps someone.

this.$set(this.contacts, newPsgId, newObj)

@SilvanFux
Copy link

this.$set()
is not long a function in vue 3

@posva
Copy link
Member

posva commented Apr 22, 2022

You don't need it in Vue 3, only in Vue 2.
Remember to use the forum or the Discord chat to ask questions!

@vuejs vuejs locked as resolved and limited conversation to collaborators Apr 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants