Home  >  Article  >  Web Front-end  >  How to add properties to vue.set

How to add properties to vue.set

coldplay.xixi
coldplay.xixiOriginal
2020-12-03 16:29:102933browse

Vue.set method to add attributes: 1. Use the [Vue.set(object, key, value)] method to add response attributes to the nested object; 2. Use [vm.$set] Instance method, the code is [this.$set(this.obj,'e',02)].

How to add properties to vue.set

The operating environment of this tutorial: Windows 7 system, Vue version 2.9.6, Dell G3 computer.

vue.set method of adding properties:

Vue does not allow dynamic addition of new root-level responsive properties on already created instances(root- level reactive property). However it is possible to add response properties to nested objects using the Vue.set(object, key, value) method:

Vue.set(vm.obj, 'e', 0)

You can also use vm.$set Instance method, which is also an alias for the global Vue.set method:

this.$set(this.obj,'e',02)

Sometimes you want to add some properties to an existing object, such as using Object.assign( ) or _.extend() method to add attributes. However, new properties added to the object do not trigger an update. In this case, you can create a new object so that it contains the properties of the original object and the new properties:

// 代替 Object.assign(this.obj, { a: 1, e: 2 })
this.obj= Object.assign({}, this.obj, { a: 1, e: 2 })

The above example is solved as follows:

How to add properties to vue.set

Click to trigger addd 3 times, click to trigger adde 3 times, the page effect and console information are as follows:

How to add properties to vue.set

##Related free learning recommendations: javascript(Video)

【Recommended related articles:

vue.js

The above is the detailed content of How to add properties to vue.set. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:When to use vue.jsNext article:When to use vue.js