vue.set in vue has 3 parameters, namely: 1. target, indicating the data source to be changed; 2. key, indicating the specific data to be changed; 3. value, indicating the reassigned value .
The operating environment of this article: windows10 system, vue2.9, Dell G3 computer.
Vue.set()
Vue.set( target, key, value )
Parameters:
target: The data source to be changed (can be an object or Array)
key: the specific data to be changed
value: the reassigned value
Official API: Vue.set()
Usage of Vue.set
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div id="app2"> <p v-for="item in items" :key="item.id"> {{item.message}} </p> <button @click="btn2Click()">动态赋值</button><br/> <button @click="btn3Click()">为data新增属性</button> </div> <script src="../../dist/vue.min.js"></script> <script> var vm2=new Vue({ el:"#app2", data:{ items:[ {message:"Test one",id:"1"}, {message:"Test two",id:"2"}, {message:"Test three",id:"3"} ] }, methods:{ btn2Click:function(){ Vue.set(this.items,0,{message:"Change Test",id:'10'}) }, btn3Click:function(){ var itemLen=this.items.length; Vue.set(this.items,itemLen,{message:"Test add attr",id:itemLen}); } } }); </script> </body> </html>
The page is like this
After I click the first button, I run the btn2Clcick method in methods. At this time, I want to change the Test one to Change Test
The result after running: At this time The Test one in the first column of the list has become Change Test
#There is a situation that you have to be wary of here:
When you get used to writing JS, there are Maybe I want to change the data in a certain subscript in the array. I just change this.items[XX], such as:
btn2Click:function(){ this.items[0]={message:"Change Test",id:'10'} }
Let’s take a look at the result:
This situation is a note clearly stated in the Vue documentation. Due to JavaScript limitations, Vue cannot detect data changes, so when we need to dynamically change the data, Vue.set() can completely meet our needs.
Students who look carefully may ask, isn’t there a button? What’s its use?
Let’s look directly:
This is the initial list data, there are three objects in the data
After clicking:
It can be seen here that Vue.set() can not only modify data, but also add data, making up for the shortcomings of Vue's array mutation method
Tip:Vue. set() can also be written as this.$set()
in methods: "vue tutorial"
The above is the detailed content of vue.set in vue has several parameters. For more information, please follow other related articles on the PHP Chinese website!

Vue.js is a progressive JavaScript framework suitable for building complex user interfaces. 1) Its core concepts include responsive data, componentization and virtual DOM. 2) In practical applications, it can be demonstrated by building Todo applications and integrating VueRouter. 3) When debugging, it is recommended to use VueDevtools and console.log. 4) Performance optimization can be achieved through v-if/v-show, list rendering optimization, asynchronous loading of components, etc.

Vue.js is suitable for small to medium-sized projects, while React is more suitable for large and complex applications. 1. Vue.js' responsive system automatically updates the DOM through dependency tracking, making it easy to manage data changes. 2.React adopts a one-way data flow, and data flows from the parent component to the child component, providing a clear data flow and an easy-to-debug structure.

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

There are the following methods to implement component jump in Vue: use router-link and <router-view> components to perform hyperlink jump, and specify the :to attribute as the target path. Use the <router-view> component directly to display the currently routed rendered components. Use the router.push() and router.replace() methods for programmatic navigation. The former saves history and the latter replaces the current route without leaving records.

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

There are two main ways to pass data in Vue: props: one-way data binding, passing data from the parent component to the child component. Events: Pass data between components using events and custom events.

Vue.js provides three ways to jump: native JavaScript API: use window.location.href to jump. Vue Router: Use the <router-link> tag or this.$router.push() method to jump. VueX: Trigger route jump through dispatch action or commit mutation.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment