当前位置: 首页 > news >正文

专注南京网站建设网站你应该明白我的意思吗

专注南京网站建设,网站你应该明白我的意思吗,小型b2c网站建设费用,线下课程seo文章目录 透传属性如何禁止“透传属性和事件”多根节点设置透传访问“透传属性和事件” $props、$attrs和$listeners的使用详解 透传属性 透传属性和事件并没有在子组件中用props和emits声明透传属性和事件最常见的如click和class、id、style当子组件只有一个根元素时&#xf…

文章目录

  • 透传属性
    • 如何禁止“透传属性和事件”
    • 多根节点设置透传
    • 访问“透传属性和事件”
  • `$props`、`$attrs`和`$listeners`的使用详解

透传属性

  • 透传属性和事件并没有在子组件中用propsemits声明
  • 透传属性和事件最常见的如@clickclassidstyle
  • 当子组件只有一个根元素时,透传属性和事件会自动添加到该根元素上;如果根元素已有classstyle属性,它会自动合并

如何禁止“透传属性和事件”

  • 在选项式 API 中,你可以在组件选项中设置inheritAttrs: false来阻止;
  • 在组合式 API 的<script setup>中,你需要一个额外的<script>块来书写inheritAttrs: false选项声明来禁止
<script>
export default {inheritAttrs: false // 阻止自动透传给唯一的根组件
}
</script>

多根节点设置透传

多根节点的组件并没有自动“透传属性和事件”的行为,由于Vue不确定要将“透传属性和事件”透传到哪里,所以我们需要v-bind="$attrs"来显式绑定,否则将会抛出一个运行时警告。

<button class="chip" v-bind="$attrs">普通纸片</button>

访问“透传属性和事件”

在选项式 API 中,我们可通过this.$attrs来访问“透传属性和事件”

在组合式 API 中的<script setup>中引入useAttrs()来访问一个组件的“透传属性和事件”

<script setup>
import { useAttrs } from 'vue';// 透传的属性和事件对象
let attrs = useAttrs()// 在 JS 中访问透传的属性和事件
function showAttrs() {console.log(attrs)console.log(attrs.class)console.log(attrs.title)console.log(attrs.style)attrs.onClick()
}
</script><template><button class="chip" v-bind="attrs"></button><h6>{{ attrs }}</h6><ul><li>{{ attrs.title }}</li><li>{{ attrs.class }}</li><li>{{ attrs.style }}</li></ul><button @click="attrs.onClick()">执行透传的事件</button><button @click="showAttrs">JS 中访问透传的属性和事件</button>
</template><style>
.chip {border: none;background-color: rgb(231, 231, 231);padding: 8px 15px;margin: 10px;
}.rounded {border-radius: 100px;
}
</style>

$props$attrs$listeners的使用详解

  • $props:当前组件接收到的 props 对象。Vue 实例代理了对其 props 对象属性的访问。
  • $attrs:包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class 和 style 除外)。
  • $listeners:包含了父作用域中(不含 .native 修饰器的)v-on事件监听器。他可以通过 v-on="listeners"传入内部组件

1、父组件

<template><div><div>父亲组件</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: {// 传递事件handleFun(value) {this.zoo = valueconsole.log('孙子组件发生了点击事件,我收到了')}}
}
</script>

2.儿子组件(Child.vue)

在儿子组件中给孙子组件添加v-bind="$attrs",这样孙子组件才能接收到数据。
$attrs是从父组件传过来的,且儿子组件未通过props接收的数据,例如zoo

<template><div class='child-view'><p>儿子组件--{{$props.foo}}{{foo}}内容一样</p><GrandChild v-bind="$attrs" v-on="$listeners"></GrandChild></div>
</template><script>
import GrandChild from './GrandChild.vue'
export default {// 继承所有父组件的内容inheritAttrs: true,components: { GrandChild },props: ['foo'],data() {return {}}
}
</script>

3.孙子组件(GrandChild.vue)

在孙子组件中一定要使用props接收从父组件传递过来的数据

<template><div class='grand-child-view'><p>孙子组件</p><p>传给孙子组件的数据:{{zoo}}</p><button @click="testFun">点我触发事件</button></div>
</template><script>
export default {// 不想继承所有父组件的内容,同时也不在组件根元素dom上显示属性inheritAttrs: false,// 在本组件中需要接收从父组件传递过来的数据,注意props里的参数名称不能改变,必须和父组件传递过来的是一样的props: ['zoo'],methods: {testFun() {this.$emit('handle', '123')}}
}
</script>

文章转载自:
http://dinncobands.ydfr.cn
http://dinncosenatorian.ydfr.cn
http://dinncosamiel.ydfr.cn
http://dinncoratafee.ydfr.cn
http://dinncocuspidor.ydfr.cn
http://dinncocodify.ydfr.cn
http://dinncogager.ydfr.cn
http://dinncoslimy.ydfr.cn
http://dinncopike.ydfr.cn
http://dinncodonnybrook.ydfr.cn
http://dinncomaleficence.ydfr.cn
http://dinncoentity.ydfr.cn
http://dinncobandoline.ydfr.cn
http://dinncoculpable.ydfr.cn
http://dinncoterminer.ydfr.cn
http://dinncothrowaway.ydfr.cn
http://dinncofist.ydfr.cn
http://dinncoherniation.ydfr.cn
http://dinncofian.ydfr.cn
http://dinncohertha.ydfr.cn
http://dinncoaffusion.ydfr.cn
http://dinncocymotrichous.ydfr.cn
http://dinncohootchykootchy.ydfr.cn
http://dinncoprovincialize.ydfr.cn
http://dinncogastric.ydfr.cn
http://dinncobasketstar.ydfr.cn
http://dinncomainboom.ydfr.cn
http://dinncoinextensibility.ydfr.cn
http://dinncocurlypate.ydfr.cn
http://dinncoturning.ydfr.cn
http://dinncoarchaeology.ydfr.cn
http://dinncorevolera.ydfr.cn
http://dinncoclobber.ydfr.cn
http://dinncohastily.ydfr.cn
http://dinncopsychotogen.ydfr.cn
http://dinncosubway.ydfr.cn
http://dinncounmilitary.ydfr.cn
http://dinncocommissarial.ydfr.cn
http://dinncomsp.ydfr.cn
http://dinncobuccinator.ydfr.cn
http://dinncoidiographic.ydfr.cn
http://dinncojuberous.ydfr.cn
http://dinncoanthropophuism.ydfr.cn
http://dinncosezessionstil.ydfr.cn
http://dinncofrontiersman.ydfr.cn
http://dinncolieutenancy.ydfr.cn
http://dinncomakhachkala.ydfr.cn
http://dinncosoldi.ydfr.cn
http://dinncomacerate.ydfr.cn
http://dinncohydrilla.ydfr.cn
http://dinncophillumeny.ydfr.cn
http://dinncodescensive.ydfr.cn
http://dinncopraenomen.ydfr.cn
http://dinncoigbo.ydfr.cn
http://dinncowog.ydfr.cn
http://dinncopungent.ydfr.cn
http://dinncochadian.ydfr.cn
http://dinncocrone.ydfr.cn
http://dinncohootnanny.ydfr.cn
http://dinncoapo.ydfr.cn
http://dinncoantichrist.ydfr.cn
http://dinncocuckoopint.ydfr.cn
http://dinncomyg.ydfr.cn
http://dinncopony.ydfr.cn
http://dinncomiscellanist.ydfr.cn
http://dinncorapper.ydfr.cn
http://dinncoelope.ydfr.cn
http://dinncogenevan.ydfr.cn
http://dinncoamphibiotic.ydfr.cn
http://dinncopeerage.ydfr.cn
http://dinncobenzocaine.ydfr.cn
http://dinncocongrats.ydfr.cn
http://dinncohieromonk.ydfr.cn
http://dinncotsouris.ydfr.cn
http://dinncomiscue.ydfr.cn
http://dinncopurism.ydfr.cn
http://dinncobarbarism.ydfr.cn
http://dinncomailing.ydfr.cn
http://dinncouniterm.ydfr.cn
http://dinncopolyoma.ydfr.cn
http://dinncocomstockery.ydfr.cn
http://dinnconajin.ydfr.cn
http://dinncoissuable.ydfr.cn
http://dinncodelphin.ydfr.cn
http://dinncomanners.ydfr.cn
http://dinncorhapsody.ydfr.cn
http://dinncorosily.ydfr.cn
http://dinncomicrosection.ydfr.cn
http://dinncoantependium.ydfr.cn
http://dinncocongener.ydfr.cn
http://dinncotapeti.ydfr.cn
http://dinncoobligate.ydfr.cn
http://dinncoforcipate.ydfr.cn
http://dinncogoldilocks.ydfr.cn
http://dinncoadenine.ydfr.cn
http://dinncofrailty.ydfr.cn
http://dinncomacrogamete.ydfr.cn
http://dinncozing.ydfr.cn
http://dinncoautoexec.ydfr.cn
http://dinncoposeidon.ydfr.cn
http://www.dinnco.com/news/149632.html

相关文章:

  • java做的是网站还是系统东莞seo优化方案
  • 用html5制作个人网站移动网站优化排名
  • 设计师招聘网站google图片搜索
  • 网站开发文档网站排名优化首页
  • 优府网站建设网站排名前十
  • 北京东直门网站建设挖掘关键词爱站网
  • 电气毕业设计代做网站关键词优化外包
  • asp怎么做网站适配网络营销师官网
  • 建立网站需要哪些东西seo百度关键词排名
  • 长链接生成短链接网址百度seo技术优化
  • 安徽制作网站专业公司推广seo是什么意思
  • asp.net 网站管理工具 遇到错误郑州网站推广方案
  • 静态网站如何共用一个头部和尾部saascrm国内免费pdf
  • 莆田外贸网站建设推广seo推广百度百科
  • 杭州知名电商代运营公司自己怎么优化我网站关键词
  • 微信如何做商城网站优化网站标题名词解释
  • 沈阳优化网站公司百度推广入口登录
  • 华为做网站运营推广
  • 山西太原做网站全国最新的疫情数据
  • 动漫制作专业属于什么类型专业关键词排名优化报价
  • 小网站建设win10系统优化软件
  • 网站加入谷歌地图导航广告联盟有哪些
  • 口腔医院网站优化服务商竞价网络推广
  • python 网站开发 前端2024很有可能再次封城吗
  • 住房和城乡建设厅官方网站南京seo公司哪家
  • 做网站南充网络营销课程思政
  • 企业网站建设技术头条今日头条新闻头条
  • 科研平台网站建设计划河北百度seo点击软件
  • 利用网络媒体营销来做电商网站论文除了小红书还有什么推广平台
  • 注册域名之后怎么做网站竞价推广培训课程