Tutorial on using $attrs and $listeners in Vue

Tutorial on using $attrs and $listeners in Vue

introduce

$attrs

Inherits all parent component properties (properties not received through props include class name and style).

inheritAttrs:

Whether non-props attributes are displayed in the outermost layer of the tag. The default value is true, which means that all parent component attributes (except props specific bindings) are inherited as normal HTML features and applied to the root element of the child component. If you do not want the root element of the component to inherit features, set inheritAttrs: false, but the class will still be inherited.

$listeners

It is an object that can receive all method bindings, which contains all listeners acting on this component. With v-on="$listeners", all event listeners are directed to a specific child element of this component.

Example

In the parent component

<template>
  <div id="app">
    <Son src="https://img01.yzcdn.cn/vant/logo.png"></Son>
  </div>
</template>
 
<script>
import Son from "./components/son.vue";
export default {
  name: "App",
  components:
    Son,
  },
};
</script>
 
<style></style>

In subcomponent

<template>
  <div id="app">
    <Son src="https://img01.yzcdn.cn/vant/logo.png"></Son>
  </div>
</template>
 
<script>
import Son from "./components/son.vue";
export default {
  name: "App",
  components:
    Son,
  },
};
</script>
 
<style></style>

It can be seen that when inheritAttrs defaults to false, the attributes are passed to the outermost subcomponent

When inheritAttrs is true

When using props to receive attributes, the attributes will not be displayed

Summary: If the attributes passed in the component tag are not received by the subcomponent, they will run to the outermost layer of the subcomponent tag.

Non-props attributes can be received through $attrs {attribute name: attribute value}

<template>
  <div>
    <img v-bind="$attrs" alt="" />
  </div>
</template>
 
<script>
export default {
  inheritAttrs: false,
};
</script>
<style scoped>
.img {
  width: 100px;
  height: 100px;
}
</style>

When binding a click event to a child component, the click event will not be triggered. You can use the .native modifier to bind successfully.

Or use $listeners to bind all methods.

In subcomponent

result

Summarize

All non-props attributes can be received through $attrs

Use: v-bind="$attrs" to bind all non-props attributes to the corresponding tags. It can also be used for components

All method binding subcomponents on the component can be received through $listeners

Use: v-on="$listeners" to bind all methods to the corresponding tags of the component, which can also be used for components

This is the end of this tutorial on how to use $attrs and $listeners in Vue. For more information about Vue $attrs $listeners, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Let's talk briefly about $attrs and $listeners of vue2.x
  • The use and difference of vue $attrs and $listeners
  • Detailed explanation of how to use $props, $attrs and $listeners in Vue
  • Vue encapsulation component tool $attrs, $listeners usage
  • Vue $attrs & inheritAttr to achieve button disabled effect case
  • Analysis of the implementation principle of $attrs and $listeners in Vue component communication
  • A comprehensive analysis of the use of vue $attrs

<<:  Detailed explanation of obtaining, assigning, and registering radio values ​​in HTML

>>:  Specific use of MySQL global locks and table-level locks

Recommend

How to install Docker on Windows 10 Home Edition

I recently used Docker to upgrade a project. I ha...

React Native environment installation process

react-native installation process 1.npx react-nat...

CSS inheritance method

Given a div with the following background image: ...

MySQL 8.0.24 installation and configuration method graphic tutorial

This article shares the installation tutorial of ...

Reasons and solutions for the failure of React event throttling effect

Table of contents The problem here is: Solution 1...

Web Design Experience: Efficiently Writing Web Code

Originally, this seventh chapter should be a deep ...

CSS modular solution

There are probably as many modular solutions for ...

Linux installation MySQL5.6.24 usage instructions

Linux installation MySQL notes 1. Before installi...

CSS to achieve the image hovering mouse folding effect

CSS to achieve the image hovering mouse folding e...

CSS implements five common 2D transformations

2D transformations in CSS allow us to perform som...

Echarts Basic Introduction: General Configuration of Bar Chart and Line Chart

1Basic steps of echarts Four Steps 1 Find the DOM...