Skip to content

Commit 2524627

Browse files
committed
翻訳前のarray-refs.mdファイルを追加
以下ファイルを追加 https://github.com/vuejs/docs-next/blob/master/src/guide/migration/array-refs.md
1 parent 42c83e9 commit 2524627

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ const sidebar = {
111111
collapsable: true,
112112
children: [
113113
'migration/introduction',
114+
'migration/array-refs',
114115
'migration/async-components',
115116
'migration/attribute-coercion',
116117
'migration/custom-directives',

src/guide/migration/array-refs.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: v-for Array Refs
3+
badges:
4+
- breaking
5+
---
6+
7+
# {{ $frontmatter.title }} <MigrationBadges :badges="$frontmatter.badges" />
8+
9+
In Vue 2, using the `ref` attribute inside `v-for` will populate the corresponding `$refs` property with an array of refs. This behavior becomes ambiguous and inefficient when there are nested `v-for`s present.
10+
11+
In Vue 3, such usage will no longer automatically create an array in `$refs`. To retrieve multiple refs from a single binding, bind `ref` to a function which provides more flexibility (this is a new feature):
12+
13+
```html
14+
<div v-for="item in list" :ref="setItemRef"></div>
15+
```
16+
17+
With Options API:
18+
19+
```js
20+
export default {
21+
data() {
22+
return {
23+
itemRefs: []
24+
}
25+
},
26+
methods: {
27+
setItemRef(el) {
28+
this.itemRefs.push(el)
29+
}
30+
},
31+
beforeUpdate() {
32+
this.itemRefs = []
33+
},
34+
updated() {
35+
console.log(this.itemRefs)
36+
}
37+
}
38+
```
39+
40+
With Composition API:
41+
42+
```js
43+
import { ref, onBeforeUpdate, onUpdated } from 'vue'
44+
45+
export default {
46+
setup() {
47+
let itemRefs = []
48+
const setItemRef = el => {
49+
itemRefs.push(el)
50+
}
51+
onBeforeUpdate(() => {
52+
itemRefs = []
53+
})
54+
onUpdated(() => {
55+
console.log(itemRefs)
56+
})
57+
return {
58+
itemRefs,
59+
setItemRef
60+
}
61+
}
62+
}
63+
```
64+
65+
Note that:
66+
67+
- `itemRefs` doesn't have to be an array: it can also be an object where the refs are set by their iteration keys.
68+
69+
- This also allows `itemRefs` to be made reactive and watched, if needed.

0 commit comments

Comments
 (0)