关于 vue2.x 的 $attrs 和 $listeners
$attrs
$attrs 用于多层次组件传递参数(组件标签的attribute,class和style除外),爷爷辈组件向孙子辈组件传递参数(注:参数不能被父辈prop识别,一旦被父辈prop识别且获取,则孙子辈组件不能获取到该参数)
写法如下:(注:v-bind不能用简写 :
)
<grand-son v-bind="$attrs" />
下面举个栗子:
爷爷(GrandFather)向父亲(Father)传递一个 msg1
向孙子(GrandSon)传递一个 msg2
,孙子会一并接收 msg1
(然而被父亲接走了,所以孙子收不到 msg1
)
<!-- GrandFather.vue -->
<template>
<div>
GrandFather:
<father :msg1="msg1" :msg2="msg2" />
</div>
</template>
<script>
import Father from './Father.vue'
export default {
components: { Father },
data() {
return {
msg1: 'msg1',
msg2: 'msg2'
}
}
}
</script>
<!-- Father.vue -->
<template>
<div>
Father: {{ msg1 }}
<grand-son v-bind="$attrs" />
</div>
</template>
<script>
import GrandSon from './GrandSon.vue'
export default {
components: { GrandSon },
props: ['msg1']
}
</script>
<!-- GrandSon.vue -->
<template>
<div>GrandSon: {{ msg1 }}{{ msg2 }}</div>
</template>
<script>
export default {
props: ['msg1', 'msg2']
}
</script>
界面现实结果:
GrandFather:
Father: msg1
GrandSon: msg2
$listeners
$listeners 用于多层次组件传递事件监听器,爷爷辈组件向父辈、孙子辈、曾孙子辈……组件传递事件(与 $attrs 不同,不存在半路被拦截的情况)
写法如下:(注:v-on 不能用简写 @
,虽然不报错,但是也不生效)
<grand-son v-on="$listeners" />
下面继续使用 爷爷-> 父亲 -> 孙子 的栗子:
爷爷(GrandFather)给父亲(Father)绑定一个 click
事件
父亲通过点击 div 触发 click
事件,同时向孙子(GrandSon)传递 $listeners
<!-- GrandFather.vue -->
<template>
<div>
GrandFather:
<father :msg1="msg1" :msg2="msg2" @click="handleClick" />
</div>
</template>
<script>
import Father from './Father.vue'
export default {
components: { Father },
data() {
return {
msg1: 'msg1',
msg2: 'msg2'
}
},
methods: {
handleClick() {
console.log('trriger click')
}
}
}
</script>
<!-- Father.vue -->
<template>
<div>
<div @click="handleFatherClick">Father: {{ msg1 }}</div>
<grand-son v-bind="$attrs" v-on="$listeners" />
</div>
</template>
<script>
import GrandSon from './GrandSon.vue'
export default {
components: { GrandSon },
props: ['msg1'],
methods: {
handleFatherClick() {
console.log('father click')
this.$emit('click')
}
}
}
</script>
<!-- GrandSon.vue -->
<template>
<div @click="handleSonClick">GrandSon: {{ msg1 }}{{ msg2 }}</div>
</template>
<script>
export default {
props: ['msg1', 'msg2'],
methods: {
handleSonClick() {
console.log('grandson click')
this.$emit('click')
}
}
}
</script>
界面:
GrandFather:
Father: msg1
GrandSon: msg2
点击 Father: msg1
,控制台显示:
father click
trriger click
点击 GrandSon: msg2
,控制台显示:
grandson click
trriger click
关于 vue2.x 的 $attrs 和 $listeners的更多相关文章
- vue2 inheritAttrs、attrs和attrs和listeners使用
inheritAttrs.attrs和attrs和listeners使用场景: 组件传值,尤其是祖孙组件有跨度的传值. (1)inheritAttrs 属性说明:https://cn.vuejs.or ...
- vue组件之间的传值——中央事件总线与跨组件之间的通信($attrs、$listeners)
vue组件之间的通信有很多种方式,最常用到的就是父子组件之间的传值,但是当项目工程比较大的时候,就会出现兄弟组件之间的传值,跨级组件之间的传值.不可否认,这些都可以类似父子组件一级一级的转换传递,但是 ...
- vue inheritAttrs、$attrs和$listeners使用
inheritAttrs.$attrs和$listeners使用场景: 组件传值,尤其是祖孙组件有跨度的传值. (1)inheritAttrs 属性说明:https://cn.vuejs.org/v2 ...
- vue组件传值之$attrs、$listeners
当有父组件A,子组件B,孙子组件C的时候 A-B B-C 的传值想必大家应该都非常熟悉了,通过props和$emit和$on来进行传值 那么A-C之间的传值要怎么做呢? 1.event.bus总线传值 ...
- Vue - 组件通信之$attrs、$listeners
前言 vue通信手段有很多种,props/emit.vuex.event bus.provide/inject 等.还有一种通信方式,那就是 $attrs 和 $listeners,之前早就听说这两个 ...
- vue中$attrs和$listeners以及inheritAttrs的用法
官方文档说明: 一.解释:包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class 和 style 除外). 意思就是父组件往子组件传没有在props里声明过的值时,子组件可以通 ...
- vue中的$props、$attrs和$listeners研究 [包装iview组件]
$props:当前组件接收到的 props 对象.Vue 实例代理了对其 props 对象属性的访问. $attrs:包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class 和 ...
- Vue组件传值(三)之 深层嵌套组件传值 - $attrs 和 $listeners
$attrs 包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class 和 style 除外).当一个组件没有声明任何 prop 时,这里会包含所有父作用域的绑定 (class和 ...
- Vue2.4+新增属性.sync、$attrs、$listeners
参考链接:https://www.jianshu.com/p/4649d317adfe
随机推荐
- CPU Cache与缓存行
编译环境:windows10+Idea+x86 CPU. 1.CPU Cache CPU 访问内存时,首先查询 cache 是否已缓存该数据.如果有,则返回数据,无需访问内存:如果不存在,则需把数据从 ...
- Codeforces Round #741 (Div. 2)
全部题目跳转链接 A - The Miracle and the Sleeper 题意 给定\([l, r]\) 求出在这个区间内的两个数字a和b的取模的最大值 (\(a \ge b\)) 分析 上届 ...
- ApacheCN Golang 译文集 20211025 更新
Go 云原生编程 零.前言 一.现代微服务架构 二.使用 RESTAPI 构建微服务 三.保护微服务 四.使用消息队列的异步微服务架构 五.使用 React 构建前端 六.在容器中部署应用 七.AWS ...
- 「YNOI2016」自己的发明
「YNOI2016」自己的发明 不换根 基本的莫队吧... 子树直接转到dfs序上. 其余部分可以见 「SNOI2017」一个简单的询问. 换根 根root,查询x,分3种: root不在x子树内,按 ...
- JS介绍、意义
HTML和CSS 京东 网页.网站和应用程序 网页:单独的一个页面 网站:一些列相关的页面组成到一起 应用程序:可以和用户产生交互,并实现某种功能. 演示JavaScript的强大 http://im ...
- HDOJ 1249 三角形『平面分隔』
很水拉 为了记规律- - 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1249 分隔平面公式 下面是我自己查找的公式,没有推到过程,但可以给一些链 ...
- termux vim 配置 自动补全 遇到的问题
// 自动不全需要安装 pkg install proot -y // 用proot可以为手机没有root的用户来模拟一个root的环境,这里主要是经典的 Linux 文件系统布局上的模拟. //执行 ...
- nodejs串行无关联
var async = require('async'); //串行无关联async.series({ one:function(cb) { setTimeout(function(){ consol ...
- ssh一段时间无操作后自动断开连接(假死)问题
平时使用ssh远程服务器的时候(注:远程虚拟机一般不会有这个问题),一段时间没有操作,ssh连接就会处于假死状态,以至于需要重新进行ssh连接,不管你用的什么远程工具都会出现这个问题,那么通过心跳检测 ...
- Solution -「CF 848D」Shake It!
\(\mathcal{Description}\) Link. 初始有一个有向图 \(G=(V,E)\),\(V=\{s,t\}\),\(E=\langle s,t\rangle\),一次操作 ...