Home Web Front-end JS Tutorial How to implement data transfer, modification and update of parent-child components in Vue (detailed tutorial)

How to implement data transfer, modification and update of parent-child components in Vue (detailed tutorial)

Jun 02, 2018 am 10:33 AM
transfer Revise data

Below I will share with you an article on the data transfer, modification and update methods of Vue parent-child components. It has a good reference value and I hope it will be helpful to everyone.

The data relationship between parent and child components, I will divide the situation into the following four types:

Parent component modifies child The data of the component, and updated in real time

The sub-component passes the data of the sub-component through $emit, this.$data refers to all the data in the data(return{...}) of the current component,

1

this.$emit('data',this.$data);

Copy after login

Then receive the data through the getinputdata method of the parent component

1

@data='getinputdata'

Copy after login

The data is the passed data. By modifying this data, the child component can be updated in real time through the parent component

1

2

3

4

5

6

7

getinputdata(data) {

 console.log(data);

 data.background = {

  backgroundColor: 'yellow',

  border: 'none'

 };

}

Copy after login

Subcomponent modifies the data of the parent component

You cannot modify the data of the parent component in the subcomponent. You can only modify the data in the parent component through the $emit method above.

You can refer to the custom events on the vue official website: https://cn.vuejs.org/v2/guide/components.html#Custom events

The child component gets the parent component data, modified but not updated in real time

1. The child component transfers the data passed by the parent component through props, and then assigns the props value to the let or var declaration variable, and then uses this variable. .

1

2

3

4

let test = this.testoutdata;

test++;

console.log(test);

console.log('test:'+this.testoutdata);

Copy after login

2. The child component transfers the data passed by the parent component through props, and then assigns the props value to the variable in data(return{...}), and then uses this variable.

1

2

3

this.outtest++;

console.log(this.outtest);

console.log('test:'+this.testoutdata);

Copy after login

You can refer to the custom events on the vue official website: https://cn.vuejs.org/v2/guide/components.html#One-way data flow

Get the parent component The data of the child component is modified but not updated in real time.

The method here is the same as the method of 'The child component obtains the data of the parent component and is modified but not updated in real time'. Among them, only the method of passing value is the difference. The child component passes the value to the parent component through $emit.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Vue.js custom event form input component method

vue.js moves the array position and updates it at the same time View method

vue.js or js method to implement Chinese A-Z sorting

The above is the detailed content of How to implement data transfer, modification and update of parent-child components in Vue (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to change the personal name in the group on DingTalk_How to modify the personal name in the group on DingTalk How to change the personal name in the group on DingTalk_How to modify the personal name in the group on DingTalk Mar 29, 2024 pm 08:41 PM

How to change the personal name in the group on DingTalk_How to modify the personal name in the group on DingTalk

Use ddrescue to recover data on Linux Use ddrescue to recover data on Linux Mar 20, 2024 pm 01:37 PM

Use ddrescue to recover data on Linux

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Apr 03, 2024 pm 12:04 PM

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation!

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Apr 01, 2024 pm 07:46 PM

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training

Can Douyin Blue V change its name? What are the steps to change the name of corporate Douyin Blue V account? Can Douyin Blue V change its name? What are the steps to change the name of corporate Douyin Blue V account? Mar 22, 2024 pm 12:51 PM

Can Douyin Blue V change its name? What are the steps to change the name of corporate Douyin Blue V account?

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Slow Cellular Data Internet Speeds on iPhone: Fixes

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times.

See all articles