This time I will show you how to use vue components for data transfer, and what are theprecautions for using vue components for data transfer. The following is a practical case, let's take a look.
Vue's component scopes are all isolated and do not allow directreferences to the data of theparent component in the template of the child component. Specific methods must be used to transfer data between components. There are roughly three situations in which data is passed between components:
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.
{{ msg }}
child
Single data flow
When the message of the parent component changes, the child component will automatically update the Method 1: Assign prop to a local variable, and then modify the local variable if you need to modify it, without affecting propexport default { data(){ return { newMessage: null } }, props: ['message'], created(){ this.newMessage = this.message; } }
export default { props: ['message'], computed: { newMessage(){ return this.message + ' 哈哈哈'; } } }
用户名:
eventto call sendUser.
用户名:{{user}}
//组件1 //组件2
page
{{msg}}
//Bus.js import Vue from 'vue' const Bus = new Vue() expore default Bus
Practical application:
import Vue from 'vue' const Bus = new Vue() var app= new Vue({ el:'#app', data:{ Bus } })
this.$root.Bus.$on() in the subcomponent , this.$root.Bus.$emit()to call
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!
Vue parent component calls the method of child component
How to bind data in dropdownlist
Introduction to laravel components
Cryptocurrency trading platform rankings
Introduction to commonly used Windows registry commands
Usage of ppf function in Python
What are the static code checking tools?
Full-width symbol input tutorial