Vue component communication: How to communicate between parent and child components?
Vue is a popular JavaScript framework that provides a component-based way to build web applications. In actual development, we often encounter situations where communication between parent and child components is required. This article will introduce some commonly used parent-child component communication methods in Vue and provide corresponding code examples.
Props is the most commonly used communication method between parent and child components in Vue. It allows parent components to pass data to child components. In child components, props are declared as an array or object, used to receive data passed by the parent component.
{{ message }}
In the above example, the parent component passes a prop named message to the child component. The subcomponent declares a property with the same name through the props array to receive the passed data. In the template of the child component, the received data can be displayed through the interpolation expression {{ message }}.
In addition to passing data from the parent component to the child component, we often also need to send data from the child component to the parent component or trigger an event. Vue provides a way for child components to communicate with parent components through Emit.
...
In the above example, the button click event in the child component triggers the updateRating method and sends a message named rating- to the parent component through this.$emit('rating-updated', rating) updated custom event, and passed the rating data rating. Use @rating-updated="updateRating" in the parent component to listen to the rating-updated event emitted by the child component and handle the event in the updateRating method.
Sometimes, we need to directly access the properties or methods of a child component from the parent component. Vue provides the $refs attribute to implement this direct access method.
Child Component
In the above example, the button click event in the parent component calls the callChildMethod method. Inside the method, this.$refs.childComponent is used to access the child component and call the childMethod method of the child component.
Provide/Inject is an advanced component communication method that allows ancestor components to provide data to all descendant components without explicitly going layer by layer. transfer. This communication method is suitable for communication between cross-level components.
...{{ message }}
In the above example, the ancestor component provides a data named message to the descendant component through the provide method. The grandson component injects this data through inject: ['message'] and displays it using {{ message }} in the template.
The above introduces the commonly used parent-child component communication methods in Vue. Each method has its applicable scenarios. In actual development, appropriate communication methods can be selected according to specific needs. I hope this article will help you understand Vue component communication!
Reference link:
The above is the detailed content of Vue component communication: How to communicate between parent and child components?. For more information, please follow other related articles on the PHP Chinese website!