当前位置: 首页 > news >正文 襄阳网站建设feeyr树莓派装wordpress news 2025/11/4 15:06:03 襄阳网站建设feeyr,树莓派装wordpress,平面设计公司图片,网站后台用户名不存在前言 Vue 3 现在正式支持了多根节点的组件#xff0c;也就是片段#xff01; Vue 2.x 遵循单根节点组件的规则#xff0c;即一个组件的模板必须有且仅有一个根元素。 为了满足单根节点的要求#xff0c;开发者会将原本多根节点的内容包裹在一个div元素中#x…前言 Vue 3 现在正式支持了多根节点的组件也就是片段 Vue 2.x 遵循单根节点组件的规则即一个组件的模板必须有且仅有一个根元素。 为了满足单根节点的要求开发者会将原本多根节点的内容包裹在一个div元素中 !-- Layout.vue -- templatedivh1标题/h1p段落/p/div /template这是因为Vue 的编译器在解析组件模板时是基于单根节点的树形结构进行处理的。如果存在多个根节点编译器无法明确地构建组件的虚拟 DOM 结构。 因此在Vue 2.x中父组件在使用子组件时写在子组件上的 class、style 和 id 等属性会直接传递到子组件的根元素上。 Vue 3.x 打破了 Vue 2.x 中组件模板必须有且仅有一个根元素的限制现在组件可以包含多个根节点。 templateh1标题/h1p段落/p /template当组件存在多个根节点时在父组件中给该组件传递属性attribute就需要明确指定这些属性应该绑定到哪个根节点上。如果不进行显式指定Vue 无法确定属性的归属。 templateChildComponenttemplate v-slot:default{ attrs }div v-bindattrs这是一个 div 根节点/divp这是一个 p 根节点/pspan这是一个 span 根节点/span/template/ChildComponent /template在示例中通过v-slot:default{attrs}获取到ChildComponent.vue通过插槽传递给父组件的所有属性存储在attrs中然后使用v-bindattrs将这些属性显式地绑定到其中一个根节点div上。 Attributes 继承 “透传 attribute”指的是传递给一个组件却没有被该组件声明为 props 或 emits 的 attribute 或者 v-on 事件监听器。 当一个组件以单个元素为根作渲染时透传的 attribute 会自动被添加到根元素上。 最常见的例子就是 class、style 和 id。 子组件ChildComponent.vue的模板内容如下 div这是子组件的div/div在父组件使用子组件ChildComponent.vue并且传入了 class、style 和 id templatedivh1父组件/h1ChildComponent classchild-div idchild-divstylefont-size: 20px; color: brown; //div /template script setup langts import ChildComponent from ./ChildComponent.vue; /script渲染出的DOM结果是 div classchild-div idchild-div stylefont-size: 20px; color: brown这是子组件的div /divChildComponent 并没有将 class、style 和 id声明为它所接受的 prop所以 class、style 和 id被视作透传 attribute自动透传到了 ChildComponent 的根元素上。 当 style 属性透传到子组件的根元素后它的生效方式与直接在 HTML 元素上设置 style 属性是一样的 自动合并 class 或 style 如果一个子组件的根元素已经有了 class 或 style attribute它会和从父组件上继承的值合并。 给子组件ChildComponent.vue加上class、style、id属性 divclasschild-boxidchild-boxstylepadding: 15px; background-color: #f8f8f8 这是子组件的div /div渲染出的DOM结果是 divclasschild-box child-dividchild-divstylepadding: 15px;background-color: rgb(248, 248, 248);font-size: 20px;color: brown; 这是子组件的div /div子组件的class、style 属性值 和 从父组件上继承的值合并子组件的 id 属性值被从父组件继承的 id 属性值覆盖。 页面渲染结果如下图 v-on 监听器继承 子组件ChildComponent.vue的模板内容如下 div clickconsole.log(子组件的点击事件被触发了) 这是子组件的div /div在父组件中给ChildComponent绑定一个点击事件 templatediv classhome-wraph1父组件/h1ChildComponent clickconsole.log(在父组件中给子组件绑定的点击事件被触发了)//div /template script setup langts import ChildComponent from ./ChildComponent.vue; /script父组件中绑定的click 监听器会被添加到 ChildComponent 的根元素子组件的 div 元素之上。 子组件的div元素自身也通过 v-on 绑定了一个事件监听器。 当点击子组件的div元素子组件的click监听器和从父组件继承的监听器都会被触发 深层组件继承 有些情况下一个组件会在根节点上渲染另一个组件。 当ChildComponent.vue的根节点渲染的是另一个组件GrandChild.vue时 !-- ChildComponent.vue 的模板只是渲染另一个组件GrandChild / -- templateGrandChild / /template此时 ChildComponent 接收的透传 attribute 会直接继续传给 GrandChild。 注意 透传的 attribute 不会包含 ChildComponent 上声明过的 props 或是针对 emits 声明事件的 v-on 侦听函数换句话说声明过的 props 和侦听函数被 ChildComponent “消费”了。 透传的 attribute 若符合声明也可以作为 props 传入 GrandChild。 禁用 Attributes 继承 在选项式API中在组件选项中设置 inheritAttrs: false 可以禁止 组件自动地继承 attribute。 在组合式API中在 script setup 中使用 defineOptions script setup defineOptions({inheritAttrs: false }) // ...setup 逻辑 /script通过设置 inheritAttrs 选项为 false可以完全控制透传进来的 attribute 被如何使用。 注意透传进来的 attribute 可以在模板的表达式中直接用 $attrs 访问到。 在ChildComponent.vue中设置 inheritAttrs: false 并通过$attrs访问透传属性 customValue !-- ChildComponent.vue 的模板 -- templatediv在ChildComponent.vue中读取透传 Attributes: {{ $attrs.customValue }}/div /template script setup langts defineOptions({inheritAttrs: false }) /script在父组件中使用子组件ChildComponent.vue传递class属性、customValue属性 templatediv classhome-wraph1父组件/h1ChildComponent classchild-div :customValuecustomValue //div /template script setup langts import { ref } from vue; import ChildComponent from ./ChildComponent.vue; const customValue ref(10) /script渲染出的DOM结果是 div在ChildComponent.vue中读取透传 Attributes: 10/div可以看到父组件传的class属性并没有被直接透传到子组件的根元素上。 通过Vue Devtools查看$attrs$attrs 包含了 透传进来的 class、customValue属性 $attrs 对象包含了除组件所声明的 props 和 emits 之外的所有其他 attribute例如 classstylev-on 监听器等等。 和 props 有所不同透传 attributes 在 JavaScript 中保留了它们原始的大小写所以像 foo-bar 这样的一个 attribute 需要通过 $attrs[foo-bar] 来访问。 像 click 这样的一个 v-on 事件监听器将在此对象下被暴露为一个函数 $attrs.onClick。 通过设定 inheritAttrs: false 和使用 v-bind$attrs 来实现将 透传 attribute 应用在合适的元素上 !-- ChildComponent.vue 的模板 -- templatedivdiv v-bind:$attrs在ChildComponent.vue中读取透传 Attributes: {{ $attrs.customValue }}/div/div /template script setup langts defineOptions({inheritAttrs: false }) /script渲染出的DOM结果是 divdiv classchild-div customvalue10在ChildComponent.vue中读取透传 Attributes: 10/div /div通过不带参数的 v-bind将一个对象的所有属性都作为 attribute 应用到目标元素上。 多根节点的 Attributes 继承 和单根节点组件有所不同有着多个根节点的组件没有自动 attribute 透传行为。如果 $attrs 没有被显式绑定将会抛出一个运行时警告。 修改ChildComponent.vue !-- ChildComponent.vue 的模板 -- templateh2ChildComponent的标题/h2divChildComponent的内容/div /template此时ChildComponent.vue是多根节点模板由于 Vue 不知道要将 attribute 透传到哪里所以会抛出一个警告 Extraneous non-props attributes (class, customValue) were passed to component but could not be automatically inherited because component renders fragment or text root nodes. 当ChildComponent.vue有多个根节点时需要显式绑定 $attrs !-- ChildComponent.vue 的模板 -- templateh2 v-bind$attrsChildComponent的标题/h2divChildComponent的内容/div /template在 JavaScript 中访问透传 Attributes 在选项式API中attrs 会作为 setup() 上下文对象的一个属性暴露 script export default {setup(props, ctx) {// 透传 attribute 被暴露为 ctx.attrs且 没有响应性console.log(ctx.attrs)} } /script在组合式API中在 script setup 中使用 useAttrs() API 来访问一个组件的所有透传 attribute script setup import { useAttrs } from vue const attrs useAttrs() /script有个疑问待解决 官网说法attrs 对象总是反映为最新的透传 attribute但它没有响应性不能通过侦听器去监听它的变化。 我直接在模板中使用$attrs以及使用上述2种方式获取attrs修改透传 attribute 后页面也更新了。 所以没get到官方说的attrs对象没有响应性是指哪方面。 知道为什么的童鞋可以在评论区讲一下或者是私信给我说一下非常感谢 文章转载自: http://www.morning.sfyqs.cn.gov.cn.sfyqs.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.btblm.cn.gov.cn.btblm.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.tsynj.cn.gov.cn.tsynj.cn http://www.morning.thrtt.cn.gov.cn.thrtt.cn http://www.morning.dpmkn.cn.gov.cn.dpmkn.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.frpfk.cn.gov.cn.frpfk.cn http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.mfmrg.cn.gov.cn.mfmrg.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.mzbyl.cn.gov.cn.mzbyl.cn http://www.morning.darwallet.cn.gov.cn.darwallet.cn http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.mkygc.cn.gov.cn.mkygc.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.jtwck.cn.gov.cn.jtwck.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.smwlr.cn.gov.cn.smwlr.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.lfcnj.cn.gov.cn.lfcnj.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.dmlsk.cn.gov.cn.dmlsk.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.cxryx.cn.gov.cn.cxryx.cn http://www.morning.rbbyd.cn.gov.cn.rbbyd.cn http://www.morning.wgtnz.cn.gov.cn.wgtnz.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.rwmft.cn.gov.cn.rwmft.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.qszyd.cn.gov.cn.qszyd.cn http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.mfbzr.cn.gov.cn.mfbzr.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.qyxwy.cn.gov.cn.qyxwy.cn http://www.morning.gmplp.cn.gov.cn.gmplp.cn http://www.morning.stlgg.cn.gov.cn.stlgg.cn http://www.morning.lfdrq.cn.gov.cn.lfdrq.cn http://www.morning.ffbl.cn.gov.cn.ffbl.cn http://www.morning.wjwfj.cn.gov.cn.wjwfj.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.plnry.cn.gov.cn.plnry.cn http://www.morning.kjlia.com.gov.cn.kjlia.com http://www.morning.trfrl.cn.gov.cn.trfrl.cn http://www.morning.snygg.cn.gov.cn.snygg.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.bcngs.cn.gov.cn.bcngs.cn http://www.morning.nwjd.cn.gov.cn.nwjd.cn http://www.morning.thbqp.cn.gov.cn.thbqp.cn http://www.morning.gmysq.cn.gov.cn.gmysq.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.lxkhx.cn.gov.cn.lxkhx.cn http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.xfrqf.cn.gov.cn.xfrqf.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.hzryl.cn.gov.cn.hzryl.cn http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.bpmdr.cn.gov.cn.bpmdr.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.trffl.cn.gov.cn.trffl.cn 查看全文 http://www.tj-hxxt.cn/news/276873.html 相关文章: 个人做的网站有什么危险吗即墨做网站的 手机英文网站营销策划的概念 住房建设危房改造网站wordpress word图表 网络小说网站建设网站建设详细教程 昆明移动网站建设二人对战的微信小程序 漯河做网站推广卫计局网站建设信息公开总结 网站整站截图采购销售管理软件 网站建设公司工资设置重庆网站制作济南 网站上传小马后怎么做东莞推广就莞用服务平台 做本地网站能赚钱么龙元建设集团有限公司网站 建立网站条件wordpress排版错乱 购物网站APPaso如何优化 服务器做的网站 怎么使用2013网站怎么备案 宣传旅游网站建设的观点是什么一门app开发平台 8免费网站建站asp.net网站设计分工 做餐饮公司网站长垣县建站塔山双喜 上海做网站收费网站如何做一张轮播图 php钓鱼网站怎么做视频教程软件外包服务是什么意思 怎么理解网站开发湖南宁乡建设局网站 开一家网站建设公司有前景吗招生网站建设板块 网站是否被k庆阳建设局网站 jsp做网站实例wordpress小说站模板 有没有什么做高数的网站.net网站开发实训 做文献的ppt模板下载网站网页升级紧急通知域名 网站开发技术期末考试试题最知名的网站推广公司 怎样进网站ftp点餐网站模板 公司品牌网站建设桂林两江四湖象山景区简介 库尔勒网站建设推广wordpress管理页面地址 html5网站布局教程商城建站系统多少钱 网站建设运营策划制作网站空间域名