[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>属性默认传递给根元素
在组件间传递时,父组件传入给子组件的属性,会默认传递给子组件的最外层标签上。
例如,在 ButtonDemo组件中导入了 Button组件,并将onClick属性传递给子组件
// ButtonDemo.vue <template> <div> <Button @click="onClick" @focus="onFoces"
size="normal">按钮</Button> </div> </template> <script lang="ts"> import Button
from '../lib/Button.vue' export default { components: {Button}, setup() { const
onClick = () => { console.log('hi') } return {onClick} } } </script> //
Button.vue<template> <button> <slot/> </button> </template>
在Button组件中未定义任何事件,但是在使用这个组件的时候传入的事件却可以使用,就是因为vue会把你传给这个组件上的所有事件,默认传给最外层的元素。
<>内层元素继承属性
如果不想让子组件最外层的元素绑定属性,则需要使用inheritAttrs:false,这样就可以让内层的元素继承属性。
// Button.vue <template> <div> <button> <slot></slot> </button> </div> </
template> <script lang="ts"> export default { inheritAttrs: false, }; </script>
<>指定元素继承属性
如果想要让div里面的button标签绑定上这些属性,可以用v-bind="$attrs"
<template> <div> <button v-bind="$attrs"> <slot/> </button> </div> </template>
<>分开继承属性
如果想要一部分绑定给button标签,一部分绑定给div标签,我们可以借助context.attrs
<template> <div :size="size"> <button v-bind="rest"> <slot></slot> </button> </
div> </template> <script lang="ts"> export default { inheritAttrs: false, setup(
props, context) { const { size, ...rest } = context.attrs; return { size, rest }
; }, }; </script>
此处,将size属性给div标签继承,剩余的属性给button标签继承。
<>总结
* 默认所有属性都绑定到根元素
* 使用 inheritAttrs:false可以取消默认绑定
* 使用$attrs或者context.attrs获取所有属性
* 使用v-bind="$attrs"批量绑定属性
* 使用 const {size, ...rest} = context.attrs将属性分开继承
<>v-bind和v-model的区别
* v-bind 是标签内属性的单向绑定
* v-model 是标签外值的双向绑定
<>props和attrs的区别
* props需要先声明才能获取值,而attrs则不用
* props声明过的属性,attrs里面不会在出现
* props不包含事件,attrs包含
* props支持string以外的类型,而attrs只有string类型