Home Web Front-end JS Tutorial How to change the props value in the vue2.0 subcomponent and pass the value to the parent component

How to change the props value in the vue2.0 subcomponent and pass the value to the parent component

Jun 02, 2018 am 10:38 AM
accomplish Change

Below I will share with you an article on how to change the props value of a vue2.0 subcomponent and pass the value to the parent component. It has a good reference value and I hope it will be helpful to everyone.

Why do we have the urge to modify the data in prop? Usually there are two reasons:

prop After it is passed in as the initial value, the sub-component wants to use it as local data;

prop is passed in as an initial value and processed by the subcomponent into other data output.

The correct response to these two reasons is:

Define a local variable and initialize it with the value of prop:

1

2

3

4

props: ['initialCounter'],

data: function () {

 return { counter: this.initialCounter }

}

Copy after login

Define a calculated property, process the value of prop and return it.

1

2

3

4

5

6

props: ['size'],

computed: {

 normalizedSize: function () {

 return this.size.trim().toLowerCase()

 }

}

Copy after login

Note that in JavaScript, objects and arrays are reference types, pointing to the same memory space. If prop

is an object or array, changing it inside the child component will affect the state of the parent component. .

We know that the parent component uses props to pass data to the child component, but how does the child component communicate with the parent component? This is where Vue's custom event system comes in handy. https://cn.vuejs.org/v2/guide/components.html#Custom events

Define two variables in the parent component and dynamically bind them to the child component

Subassembly:

Parent component: Define two methods and bind them to the child component custom event

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

Related articles:

Sample code for actual node static file server

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

vue.js method of moving the array position and updating the view at the same time

The above is the detailed content of How to change the props value in the vue2.0 subcomponent and pass the value to the parent component. 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

Hot Article

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 implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones?

Use Java to write code to implement love animation Use Java to write code to implement love animation Dec 23, 2023 pm 12:09 PM

Use Java to write code to implement love animation

How to change the starting delivery price of Meituan Takeout merchant version How to change the starting delivery price of Meituan Takeout merchant version Mar 27, 2024 pm 07:20 PM

How to change the starting delivery price of Meituan Takeout merchant version

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

PHP Programming Guide: Methods to Implement Fibonacci Sequence

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

Master how Golang enables game development possibilities

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks

See all articles