- 一、 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 生命周期事件名
- 二十一、侦听数组
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
一、 v-for 中的 Ref 数组
Vue 2.x
在 Vue 2 中,在 v-for
中使用的 ref
attribute 会用 ref 数组填充相应的 $refs
property
<template>
<div v-for="item in list" ref="setItemRef"></div>
<template>
<script>
export default {
methods: {
ItemRef() {
console.log(this.$refs.setItemRef); // [div, div, div, ...]
}
}
}
</script>
Vue 3.x
在 Vue 3 中,此类用法将不再自动创建 $ref
数组。要从单个绑定获取多个 ref,将 ref
绑定到函数上
注意:
itemRefs
不必是数组:它也可以是一个对象,其 ref 可以通过迭代的 key 被设置。- 如有需要,
itemRefs
也可以是响应式的,且可以被侦听。
<template>
<div v-for="item in list" :ref="setItemRef"></div>
<template>
<!-- 结合选项式 API -->
<script>
export default {
data() {
return {
itemRefs: []
}
},
methods: {
setItemRef(el) {
if (el) {
this.itemRefs.push(el)
}
}
},
beforeUpdate() {
this.itemRefs = []
},
updated() {
console.log(this.itemRefs) // Proxy {0: div, 1: div, .....}
}
}
</script>
<!-- 结合组合式 AP -->
import { onBeforeUpdate, onUpdated } from 'vue'
export default {
setup() {
let itemRefs = []
const setItemRef = el => {
if (el) {
itemRefs.push(el)
}
}
onBeforeUpdate(() => {
itemRefs = []
})
onUpdated(() => {
console.log(itemRefs)
})
return {
setItemRef
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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