- 一、 v-for 中的 Ref 数组
- 二、插槽统一
- 三、attribute 强制行为
- 四、 $attrs 包含 class & style
- 五、自定义指令
- 六、与自定义元素的互操作性
- 七、Data 选项
- 八、函数式组件
- 九、全局 API
- 十、 key Attribute
- 十一、按键修饰符
- 十二、在 prop 的默认函数中访问 this
- 十三、渲染函数 API
- 十四、transition 组件过渡的 class 名更改
- 十五、Transition 作为根节点
- 十六、Transition Group 根元素
- 十七、v-model
- 十八、v-if 与 v-for 的优先级对比
- 十九、v-bind 合并行为
- 二十、VNode 生命周期事件名
- 二十一、侦听数组
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
四、 $attrs 包含 class & style
Vue2.x
$attrs
现在包含了所有传递给组件的 attribute,但不包括 class
和 style
。
上述行为在使用 inheritAttrs: false
时会产生副作用:
$attrs
中的 attribute 将不再被自动添加到根元素中,而是由开发者决定在哪添加。- 但是
class
和style
不属于$attrs
,它们仍然会被应用到组件的根元素中:
<template>
<label>
<input type="text" v-bind="$attrs" />
</label>
</template>
<script>
export default {
inheritAttrs: false
}
</script>
像这样使用时:
<my-component id="my-id" class="my-class"></my-component>
将生成以下 HTML:
<label class="my-class">
<input type="text" id="my-id" />
</label>
Vue3.x
$attrs
现在包含了所有传递给组件的 attribute,包括 class
和 style
。
$attrs
包含了所有的 attribute,这使得把它们全部应用到另一个元素上变得更加容易了。现在上面的示例将生成以下 HTML:
<label>
<input type="text" id="my-id" class="my-class" />
</label>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论