backgroundNow let's discuss a situation, how do parent components communicate with grandchild components, how many solutions do we have?
1. Document Description(1) $props : The props object received by the current component. A Vue instance proxies access to the properties of its props object. (2) $attrs : Contains attribute bindings (except class and style) that are not recognized (and obtained) as props in the parent scope. (3) $listeners : Contains the v-on event listeners in the parent scope (without the .native modifier). It can be passed into the internal component through v-on="listeners" 2. Specific Use1. Parent component <template> <div> <div>Father component</div> <Child :foo="foo" :zoo="zoo" @handle="handleFun" > </Child> </div> </template> <script> import Child from './Child.vue' export default { components: { Child }, data() { return { foo: 'foo', zoo: 'zoo' } }, methods: { // Pass event handleFun(value) { this.zoo = value console.log('A click event occurred in the grandchild component, and I received it') } } } </script> 2. Child component (Child.vue) The middle layer, as a transmission intermediary between the parent component and the grandchild component, adds
<template> <div class='child-view'> <p>Son component - {{$props.foo}} has the same content as {{foo}}</p> <GrandChild v-bind="$attrs" v-on="$listeners"></GrandChild> </div> </template> <script> import GrandChild from './GrandChild.vue' export default { // Inherit all parent component contents inheritAttrs: true, components: { GrandChild }, props: ['foo'], data() { return { } } } </script> 3. Grandchild component (GrandChild.vue) In the grandchild component, you must use props to receive data passed from the parent component <template> <div class='grand-child-view'> <p>Grandchild component</p> <p>Data passed to grandchild component: {{zoo}}</p> <button @click="testFun">Click me to trigger the event</button> </div> </template> <script> export default { // Do not want to inherit all parent component contents, and do not display attributes on the component root element DOM inheritAttrs: false, // In this component, you need to receive the data passed from the parent component. Note that the parameter name in props cannot be changed. It must be the same as the props passed by the parent component: ['zoo'], methods: { testFun() { this.$emit('handle', '123') } } } </script> ConclusionFrom the above code, we can see that using the attrs and inheritAttrs attributes can use concise code to pass the data of component A to component C. The scope of use of this scenario is quite wide. Through listeners , we bind v-on="$listeners" on component b, and in component a, listen to the events triggered by component c. The data sent by component c can be passed to component a. This is the end of this article about the detailed usage of $props, $attrs and $listeners in Vue. For more relevant Vue $props, $attrs and $listeners content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: About WSL configuration and modification issues in Docker
>>: HTML code to add icons to transparent input box
Table of contents 1. Background running jobs 2. U...
As shown below: //Query the year and month of the...
When I configured mysql, I set the default storag...
Table of contents 1. Template 2. Generics 3. Gene...
1. Check whether the existing nginx supports ipv6...
Table of contents Overview Method 1: Pass paramet...
Table of contents 1. Subquery definition 2. Subqu...
The purpose of writing scripts is to avoid having...
If every company wants to increase its user base,...
Recently, I learned about the Vue project and cam...
Click here to return to the 123WORDPRESS.COM HTML ...
GitHub address: https://github.com/dmhsq/dmhsq-my...
A problem occurred when configuring a cluster. Or...
1. About the file server In a project, if you wan...
What is a mirror? An image can be seen as a file ...