当前位置:网站首页>$attrs/$listeners
$attrs/$listeners
2022-07-31 03:32:00 【The elephant and small ant's life】
如上图所示 $attrs 表示没有继承数据的对象,格式为 {
属性名:属性值}.Vue2.4 提供了 $attrs , $listeners 来传递数据与事件,跨级组件之间的通讯变得更简单.
简单来说:$attrs 与 $listeners 是两个对象,$attrs 里存放的是父组件中绑定的非 Props 属性,$listeners 里存放的是父组件中绑定的非原生事件.
1、简介
- 多级组件嵌套需要传递数据时,通常使用的方法是通过 vuex.
- 但如果仅仅是传递数据,而不做中间处理,使用 vuex 处理,未免有点大材小用.为此 Vue2.4 版本提供了另一种方法---- a t t r s / attrs/ attrs/listeners
2、$attrs
- 包含了父作用域中不被 prop 所识别 (且获取) 的特性绑定 (class 和 style 除外).
- 当一个组件没有声明任何 prop 时,这里会包含所有父作用域的绑定 (class 和 style 除外),并且可以通过 v-bind=“$attrs” 传入内部组件.通常配合 interitAttrs 选项一起使用.
有关 interitAttrs 详情可见 Blog Garden Lu Xiaofenglyf 的文章vue组件的inheritAttrs属性
3、$listeners
- 包含了父作用域中的 (不含 .native 修饰器的) v-on 事件监听器.
- 它可以通过 v-on=“$listeners” 传入内部组件.
4、Next we look at an example of cross-level communication
// index.vue
<template>
<div>
<h2>浪里行舟</h2>
<child-com1
:foo="foo"
:boo="boo"
:coo="coo"
:doo="doo"
title="前端工匠"
></child-com1>
</div>
</template>
<script>
const childCom1 = () => import("./childCom1.vue");
export default {
components: {
childCom1 },
data() {
return {
foo: "Javascript",
boo: "Html",
coo: "CSS",
doo: "Vue"
};
}
};
</script>
// childCom1.vue
<template class="border">
<div>
<p>foo: {
{
foo }}</p>
<p>childCom1的$attrs: {
{
$attrs }}</p>
<child-com2 v-bind="$attrs"></child-com2>
</div>
</template>
<script>
const childCom2 = () => import("./childCom2.vue");
export default {
components: {
childCom2
},
inheritAttrs: false, // 可以关闭自动挂载到组件根元素上的没有在 props 声明的属性
props: {
foo: String // foo 作为 props 属性绑定
},
created() {
console.log(this.$attrs);
// { "boo": "Html", "coo": "CSS", "doo": "Vue", "title": "前端工匠" }
}
};
</script>
// childCom2.vue
<template>
<div class="border">
<p>boo: {
{
boo }}</p>
<p>childCom2: {
{
$attrs }}</p>
<child-com3 v-bind="$attrs"></child-com3>
</div>
</template>
<script>
const childCom3 = () => import("./childCom3.vue");
export default {
components: {
childCom3
},
inheritAttrs: false,
props: {
boo: String
},
created() {
console.log(this.$attrs);
// {"coo": "CSS", "doo": "Vue", "title": "前端工匠" }
}
};
</script>
// childCom3.vue
<template>
<div class="border">
<p>childCom3: {
{
$attrs }}</p>
</div>
</template>
<script>
export default {
props: {
coo: String,
title: String
}
};
</script>

** 如上图所示 $attrs 表示没有继承数据的对象,格式为 {属性名:属性值}.Vue2.4 提供了 $attrs , $listeners 来传递数据与事件,跨级组件之间的通讯变得更简单.**
简单来说:$attrs 与 l i s t e n e r s 是两个对象, listeners 是两个对象, listeners是两个对象,attrs 里存放的是父组件中绑定的非 Props 属性,$listeners 里存放的是父组件中绑定的非原生事件.
5、使用
<el-table
ref="elTable"
:height="tableHeight"
v-bind="$attrs"
v-on="$listeners"
:data="tableData"
>
边栏推荐
- Ambiguous method call.both
- A brief introduction to the showDatePicker method of the basic components of Flutter
- 【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
- Problems that need to be solved in distributed system architecture
- Automation strategies for legacy systems
- 【CocosCreator 3.5】CocosCreator 获取网络状态
- MultipartFile file upload
- 原子操作 CAS
- [Compilation principle] Lexical analysis program design principle and implementation
- 浅识Flutter 基本组件之CheckboxListTile组件
猜你喜欢

VS QT - ui does not display newly added members (controls) || code is silent

Recursive query single table - single table tree structure - (self-use)

Detailed explanation of TCP and UDP

IDEA comment report red solution

How to develop a high-quality test case?

Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)
![[C language] Three-pointed chess (classic solution + list diagram)](/img/64/18ed08b64f9618bbd7f24ee16e6a6f.jpg)
[C language] Three-pointed chess (classic solution + list diagram)

Mysql 45 study notes (twenty-five) MYSQL guarantees high availability

从滴滴罚款后数据治理思考

Based on the local, linking the world | Schneider Electric "Industrial SI Alliance" joins hands with partners to go to the future industry
随机推荐
Detailed explanation of TCP (3)
(线段树) 基础线段树常见问题总结
Daily practice of LeetCode - 138. Copy a linked list with random pointers
With 7 years of experience, how can functional test engineers improve their abilities step by step?
QML的使用
IDEA 注释报红解决
[Godot][GDScript] 二维洞穴地图随机生成
MultipartFile file upload
【AUTOSAR-RTE】-5-Explicit(显式)和Implicit(隐式) Sender-Receiver communication
BP神经网络
Mysql 45 study notes (twenty-four) MYSQL master-slave consistency
Distributed locks and three implementation methods
addressable in Golang
[C language] Three-pointed chess (classic solution + list diagram)
C语言从入门到如土——数据的存储
A brief introduction to the CheckboxListTile component of the basic components of Flutter
7年经验,功能测试工程师该如何一步步提升自己的能力呢?
点云DBSCAN聚类(MATLAB,非内置函数)
The use of beforeDestroy and destroyed
Why SocialFi achievement Web3 decentralized social in the future