The parent component passes data to the child component, and the data is passed through props.
The child component passes data to the parent component and passes the data through events.
Transfer data between two sibling components through event bus.
1. Parent component passes data to child component
Child component part:
{{ msg }}
Copy after login
In child. In vue, msg is a variable defined in data. Use props: ['msg'] to get the value of msg from the parent component
Parent component part:
child
Copy after login
When calling the component, Use v-bind to bind the value of msg to the variable message defined in parent.vue, so that the value of message in parent.vue can be passed to child.vue.
Single data flow
When the message of the parent component changes, the child component will automatically update the
2. The child component passes data to the parent component
The subcomponent mainly passes data to the parent component through practice.
The subcomponent part:
用户名:
Copy after login
In the html of the subcomponent, when the value in the input changes, the username is passed to parent.vue.
First declare a sendUser method and use change
In sendUser, use $emit to traverse the changeName event and return this.name, where changeName is a custom event, which functions like a relay. This.name will be passed to the parent through this event components.
Parent component part:
用户名:{{user}}
Copy after login
Declare a getUser method in the parent component, use the changeName event to call the getUser method, and obtain the parameter username passed from the child component
Copy after login
The parameter msg in the getUser method is the parameter uesrname passed from the subcomponent.
3. Data transfer between components at the same level
Sometimes two components also need to communicate (non-parent-child relationship). Of course Vue2.0 provides Vuex, but in simple scenarios, an empty Vue instance can be used as the central event bus.
//组件1 //组件2
Copy after login
In component c1:
page
Copy after login
In component c2:
{{msg}}
Copy after login
In actual application, the bus is generally extracted:
//Bus.js import Vue from 'vue' const Bus = new Vue() expore default Bus
Copy after login
Component call When referenced (import Bus from './Bus.js')
However, in this way of introduction, the local scope of the Bus may appear after being packaged by webpack, that is, two different Buses are referenced. Resulting in normal communication
Practical application:
Inject Bus into the Vue root object
import Vue from 'vue' const Bus = new Vue() var app= new Vue({ el:'#app', data:{ Bus } })
Copy after login
Pass
this.$root.Bus.$on() in the subcomponent , this.$root.Bus.$emit()to call
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use WebPack to configure vue multi-page
How to use the Node.js sandbox environment
The above is the detailed content of How to use vue components for data transfer. For more information, please follow other related articles on the PHP Chinese 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