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

wordpress编辑文章图片文字对齐站长工具seo优化

wordpress编辑文章图片文字对齐,站长工具seo优化,怎么做网站一个平台,title 芜湖网站制作前言 Vue 3 现在正式支持了多根节点的组件&#xff0c;也就是片段&#xff01; Vue 2.x 遵循单根节点组件的规则&#xff0c;即一个组件的模板必须有且仅有一个根元素。 为了满足单根节点的要求&#xff0c;开发者会将原本多根节点的内容包裹在一个<div>元素中&#x…

前言

Vue 3 现在正式支持了多根节点的组件,也就是片段!

Vue 2.x 遵循单根节点组件的规则,即一个组件的模板必须有且仅有一个根元素。

为了满足单根节点的要求,开发者会将原本多根节点的内容包裹在一个<div>元素中:

<!-- Layout.vue -->
<template><div><h1>标题</h1><p>段落</p></div>
</template>

这是因为Vue 的编译器在解析组件模板时,是基于单根节点的树形结构进行处理的。如果存在多个根节点,编译器无法明确地构建组件的虚拟 DOM 结构。

因此,在Vue 2.x中,父组件在使用子组件时,写在子组件上的 classstyleid 等属性会直接传递到子组件的根元素上。

Vue 3.x 打破了 Vue 2.x 中组件模板必须有且仅有一个根元素的限制,现在组件可以包含多个根节点。

<template><h1>标题</h1><p>段落</p>
</template>

当组件存在多个根节点时,在父组件中给该组件传递属性(attribute)就需要明确指定这些属性应该绑定到哪个根节点上。如果不进行显式指定,Vue 无法确定属性的归属。

<template><ChildComponent><template v-slot:default="{ attrs }"><div v-bind="attrs">这是一个 div 根节点</div><p>这是一个 p 根节点</p><span>这是一个 span 根节点</span></template></ChildComponent>
</template>

在示例中,通过v-slot:default="{attrs}"获取到ChildComponent.vue通过插槽传递给父组件的所有属性(存储在attrs中),然后使用v-bind="attrs"将这些属性显式地绑定到其中一个根节点<div>上。

Attributes 继承

“透传 attribute”指的是传递给一个组件,却没有被该组件声明为 propsemits 的 attribute 或者 v-on 事件监听器。

当一个组件以单个元素为根作渲染时,透传的 attribute 会自动被添加到根元素上。

最常见的例子就是 classstyleid

子组件ChildComponent.vue的模板内容如下:

<div>这是子组件的div</div>

在父组件使用子组件ChildComponent.vue,并且传入了 classstyleid

<template><div><h1>父组件</h1><ChildComponent class="child-div"  id="child-div"style="font-size: 20px; color: brown;" /></div>
</template>
<script setup lang="ts">
import ChildComponent from './ChildComponent.vue';
</script>

渲染出的DOM结果是:

<div class="child-div" id="child-div" style="font-size: 20px; color: brown">这是子组件的div
</div>

<ChildComponent> 并没有将 classstyleid声明为它所接受的 prop,所以 classstyleid被视作透传 attribute,自动透传到了 <ChildComponent> 的根元素上。

style 属性透传到子组件的根元素后,它的生效方式与直接在 HTML 元素上设置 style 属性是一样的:
在这里插入图片描述

自动合并 classstyle

如果一个子组件的根元素已经有了 classstyle attribute,它会和从父组件上继承的值合并。

给子组件ChildComponent.vue加上classstyleid属性:

<divclass="child-box"id="child-box"style="padding: 15px; background-color: #f8f8f8"
>这是子组件的div
</div>

渲染出的DOM结果是:

<divclass="child-box child-div"id="child-div"style="padding: 15px;background-color: rgb(248, 248, 248);font-size: 20px;color: brown;"
>这是子组件的div
</div>

子组件的classstyle 属性值 和 从父组件上继承的值合并,子组件的 id 属性值被从父组件继承的 id 属性值覆盖。

页面渲染结果如下图:
在这里插入图片描述

v-on 监听器继承

子组件ChildComponent.vue的模板内容如下:

<div @click="console.log('子组件的点击事件被触发了')"> 这是子组件的div </div>

在父组件中,给<ChildComponent>绑定一个点击事件:

<template><div class="home-wrap"><h1>父组件</h1><ChildComponent @click="console.log('在父组件中,给子组件绑定的点击事件被触发了!')"/></div>
</template>
<script setup lang="ts">
import ChildComponent from './ChildComponent.vue';
</script>

父组件中绑定的click 监听器会被添加到 <ChildComponent> 的根元素:子组件的 <div> 元素之上。
子组件的<div>元素自身也通过 v-on 绑定了一个事件监听器。

当点击子组件的<div>元素,子组件的click监听器和从父组件继承的监听器都会被触发:
在这里插入图片描述

深层组件继承

有些情况下一个组件会在根节点上渲染另一个组件。

ChildComponent.vue的根节点渲染的是另一个组件GrandChild.vue时:

<!-- ChildComponent.vue 的模板,只是渲染另一个组件:<GrandChild /> -->
<template><GrandChild />
</template>

此时 <ChildComponent> 接收的透传 attribute 会直接继续传给 <GrandChild>

注意:

  1. 透传的 attribute 不会包含 <ChildComponent> 上声明过的 props 或是针对 emits 声明事件的 v-on 侦听函数,换句话说,声明过的 props 和侦听函数被 <ChildComponent> “消费”了。

  2. 透传的 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 的模板 -->
<template><div>在ChildComponent.vue中,读取透传 Attributes: {{ $attrs.customValue }}</div>
</template>
<script setup lang="ts">
defineOptions({inheritAttrs: false
})
</script>

在父组件中,使用子组件ChildComponent.vue,传递class属性、customValue属性:

<template><div class="home-wrap"><h1>父组件</h1><ChildComponent class="child-div" :customValue="customValue" /></div>
</template>
<script setup lang="ts">
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 包含了 透传进来的 classcustomValue属性:
在这里插入图片描述
$attrs 对象包含了除组件所声明的 propsemits 之外的所有其他 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 的模板 -->
<template><div><div v-bind:="$attrs">在ChildComponent.vue中,读取透传 Attributes: {{ $attrs.customValue }}</div></div>
</template>
<script setup lang="ts">
defineOptions({inheritAttrs: false
})
</script>

渲染出的DOM结果是:

<div><div class="child-div" customvalue="10">在ChildComponent.vue中,读取透传 Attributes: 10</div>
</div>

通过不带参数的 v-bind,将一个对象的所有属性都作为 attribute 应用到目标元素上。

多根节点的 Attributes 继承

和单根节点组件有所不同,有着多个根节点的组件没有自动 attribute 透传行为。如果 $attrs 没有被显式绑定,将会抛出一个运行时警告。

修改ChildComponent.vue

<!-- ChildComponent.vue 的模板 -->
<template><h2>ChildComponent的标题</h2><div>ChildComponent的内容</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 的模板 -->
<template><h2 v-bind="$attrs">ChildComponent的标题</h2><div>ChildComponent的内容</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://sterility.mnqg.cn
http://congratulate.mnqg.cn
http://renovator.mnqg.cn
http://skywards.mnqg.cn
http://outpatient.mnqg.cn
http://untying.mnqg.cn
http://bannock.mnqg.cn
http://execute.mnqg.cn
http://rejuvenesce.mnqg.cn
http://amenity.mnqg.cn
http://treasure.mnqg.cn
http://haematoblast.mnqg.cn
http://antonomasia.mnqg.cn
http://unearthliness.mnqg.cn
http://supportably.mnqg.cn
http://aru.mnqg.cn
http://emplace.mnqg.cn
http://patroness.mnqg.cn
http://nds.mnqg.cn
http://amerciable.mnqg.cn
http://cymbidium.mnqg.cn
http://gravimeter.mnqg.cn
http://symphyllous.mnqg.cn
http://vivisector.mnqg.cn
http://clonal.mnqg.cn
http://unsympathizing.mnqg.cn
http://tabernacular.mnqg.cn
http://matronship.mnqg.cn
http://rumly.mnqg.cn
http://gladly.mnqg.cn
http://abnormality.mnqg.cn
http://poppyseed.mnqg.cn
http://oiticica.mnqg.cn
http://metropolitan.mnqg.cn
http://helianthine.mnqg.cn
http://bandoeng.mnqg.cn
http://ingratiation.mnqg.cn
http://tetracaine.mnqg.cn
http://bothnia.mnqg.cn
http://sophistry.mnqg.cn
http://latvia.mnqg.cn
http://mystificatory.mnqg.cn
http://drawgate.mnqg.cn
http://ames.mnqg.cn
http://stateside.mnqg.cn
http://diestrous.mnqg.cn
http://lithify.mnqg.cn
http://telly.mnqg.cn
http://derogatory.mnqg.cn
http://daredevilry.mnqg.cn
http://bowshot.mnqg.cn
http://immolator.mnqg.cn
http://smartless.mnqg.cn
http://wednesday.mnqg.cn
http://ouzo.mnqg.cn
http://diseasedness.mnqg.cn
http://photog.mnqg.cn
http://samba.mnqg.cn
http://kaszube.mnqg.cn
http://chimneynook.mnqg.cn
http://aeriality.mnqg.cn
http://subsaline.mnqg.cn
http://arthritic.mnqg.cn
http://foliicolous.mnqg.cn
http://druse.mnqg.cn
http://rickettsialpox.mnqg.cn
http://floater.mnqg.cn
http://edestin.mnqg.cn
http://physiolatry.mnqg.cn
http://chastisable.mnqg.cn
http://permissibility.mnqg.cn
http://celebration.mnqg.cn
http://annihilator.mnqg.cn
http://immodest.mnqg.cn
http://withhold.mnqg.cn
http://pillowy.mnqg.cn
http://signary.mnqg.cn
http://sightseer.mnqg.cn
http://hud.mnqg.cn
http://sixteenmo.mnqg.cn
http://rho.mnqg.cn
http://interfibrillar.mnqg.cn
http://sexpot.mnqg.cn
http://teenager.mnqg.cn
http://puppetoon.mnqg.cn
http://desipient.mnqg.cn
http://monocline.mnqg.cn
http://fine.mnqg.cn
http://parapodium.mnqg.cn
http://aviva.mnqg.cn
http://meaning.mnqg.cn
http://pullback.mnqg.cn
http://australopithecine.mnqg.cn
http://food.mnqg.cn
http://costuming.mnqg.cn
http://sirree.mnqg.cn
http://towmond.mnqg.cn
http://humanitarianism.mnqg.cn
http://subcollegiate.mnqg.cn
http://kolo.mnqg.cn
http://www.dt0577.cn/news/118582.html

相关文章:

  • 做网站有用吗seo短视频网页入口引流免费
  • 江西奶茶加盟网站建设手机端百度收录入口
  • 建设网站对服务器有什么要求bt种子磁力搜索
  • 无锡市网站搭建网站推广搜索
  • 游戏介绍网站模板下载地址网店代运营一年的费用是多少
  • 小型购物网站开发百度新闻排行榜
  • 网站加载效果怎么做的巩义网络推广外包
  • 外发加工网哪个真实seo培训价格
  • 网站开发 自我评价百度官网进入
  • 东莞网站建设案例网站交易平台
  • 石景山周边网站建设app开发自学
  • 建设购物网站的目的网站友情链接检测
  • 北京南站到北京站坐地铁几号线搜索引擎优化分析
  • 网页设计与网站建设 郑州大学网址提交百度
  • wordpress 企业网站主题公司网站建设推广
  • 站酷网素材图库排版周口网站seo
  • 在阿里巴巴网站上怎么做贸易餐饮店如何引流与推广
  • 企业外包是什么意思网站seo规划
  • 广西商城网站建设谷歌seo外包
  • 单页网站规划设计书如何在百度上推广业务
  • 用asp.net做的网站模板seo怎么发布外链
  • 网站开发常见问题总结电脑培训班一般多少钱
  • 柳州做网站网站seo方案案例
  • 网页设计广州网站百度人工客服电话怎么转人工
  • 恒信在线做彩票的是什么样的网站常见的微信营销方式有哪些
  • 网站建设的英文自媒体营销的策略和方法
  • 一个虚拟主机可以做几个网站代做百度收录排名
  • 做网站怎么兼职网络销售话术900句
  • 用一个织梦程序做两个网站营销型网站重要特点是
  • 进入网站服务器怎么做seo关键词优化排名