首页 > 其他分享 >$attrs和$listeners

$attrs和$listeners

时间:2023-10-30 21:13:44浏览次数:32  
标签:传递数据 listeners attrs 组件 方法 属性

用于场景:
放在中间组件中用于跨组件传递数据和方法,相当于把所有的属性和方法中转给下一级组件
\(attrs:用于向下传递数据变量 \)listeners:用于向下传递祖组件的方法
祖:A 父:B 孙:C
需要在B组件中使用\(attrs和\)listeners中转
例如:

//A: A中有个a属性和b属性和fun方法,传给了子组件B,selfAttr是A的自定义特性
<div>
	<B :a="a" :a="a" @fun="fun" selfAttr="myself"/>
</div>

//B:通过$attrs和$listeners将所有的属性和方法传承下去
//由于$attrs会将所有attr都继承下去,对于不需要的selfAttr可以在B组件中设置inheritAttrs: false,与data同级别
<div>
	<C v-bind="$attr" v-on="$listeners"/>
</div>
...
 inheritAttrs: true,
  mounted() {
    console.log('this.$attrs', this.$attrs)
  }

//C:可以通过B组件拿到祖父A组件的属性和调用方法,与父子传参一样
<div>
	{{}}
</div>
...
props:{
a:String,
b:String
}
//调用方法
this.$emit('fun')

标签:传递数据,listeners,attrs,组件,方法,属性
From: https://www.cnblogs.com/a-fairy/p/17798822.html

相关文章

  • vue中attrs的使用
    vue中attrs的使用1.attrs的作用用来进行子孙组件之间的数据传递接收父组件传过来,但是又没有在props中定义的数据。(class及style除外)2.父子组件之间数据传递的用法爷爷组件-grandpa:<template><div><span>爷爷</span><Son:phone="phone"sex="男"......
  • v-bind="$attrs" v-on="$listeners"
    v-bind="$attrs"主要用于组件之间的隔代传值。例如有父组件A,子组件B,孙组件C三个组件。A组件中的值需要直接传给C,那么就需要在B中设置v-bind="$attrs",然后在C组件中用prop接收,此时就直接把值传给了C组件。父组件A<template><B_zujianmsg='123'/></template>子组件B......
  • 【解决】idea启动spring MVC报错:一个或多个listeners启动失败Listener ClassNotFoundE
    idea配置教程。tomcat调试报错Artifact:warexploded:Errorduringartifactdeployment。修改代码后,启动不生效,仍是旧代码。根本原因是:Modulesoutputpath和Artifactsoutputdirectory不匹配Modulesoutputpath一定要等于Artifactsoutputdirectory加\WEB-INF\classes......
  • startInternal One or more listeners failed to start
    IDEA启动项目一直报错startInternalOneormorelistenersfailedtostart,修改了tomcat端口号也不行,网上各种方法尝试也不好使。这时关注IDEA启动TOMCATLOCALHOSTlog日志,查看日志中报错信息,针对性修改错误......
  • listeners和v-model
    <template> <divid="app">  <LoadingButton@click="handlesClick"></LoadingButton>  <ceShi2></ceShi2> </div></template><script>importLoadingButtonfrom'@/compone......
  • [Debug] Debug and inspect event listeners with Devtools
    Youcanuse getEventListeners(button)directlyinsidechromedevtool,butnotinsideapplicationcode. Youcanuse monitorEvents(button,'keydown'),noweverytimeskeydownhappens,eventwillbeloggedintotheconsole.Use unmonitorEvents(but......
  • 【SpringBoot】【二】 SpringApplicationRunListeners 监听器执行过程详解
    1 前言我们看到SpringBoot启动的时候,会在每个时机执行监听器,这节我们就来看看,加载监听器的过程我们就不说了哈,上节说过了哈,本节我们主要看:(1)SpringApplicationRunListeners的创建过程(2)监听器的执行时机有哪些(3)监听器的执行过程三个方面来看哈。2 使用在看之前,我们先......
  • Vue中 $attrs、$listeners 详解及使用
    传送门:Vue中子组件向父组件传值及.sync修饰符详解传送门:Vue中状态管理器(vuex)详解及应用场景传送门:Vue中事件总线(eventBus)详解及使用传送门:Vue中provide、inject详解及使用Vue中常见的组件通信方式可分为三类父子通信父向子传递数据是通过props,子向父是通过events($emit);......
  • 第十九篇 vue - 深入组件 - 透传 Attributes - $attrs
    Attributes继承“透传attribute”指的是传递给一个组件,却没有被该组件声明为props或emits的attribute或者v-on事件监听器。最常见的例子就是class、style和id当一个组件以单个元素为根作渲染时,透传的attribute会自动被添加到根元素上。举例来说,假如我们有一个<......
  • ### 类的私有属性 **__private_attrs**:两个下划线开头,声明该属性为私有,不能在类的外
    1classDemo():2__num=0#私有属性3result=0#公开属性45defcount(self):6self.__num+=17self.result+=1......