File tree 2 files changed +70
-0
lines changed
2 files changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -111,6 +111,7 @@ const sidebar = {
111
111
collapsable : true ,
112
112
children : [
113
113
'migration/introduction' ,
114
+ 'migration/array-refs' ,
114
115
'migration/async-components' ,
115
116
'migration/attribute-coercion' ,
116
117
'migration/custom-directives' ,
Original file line number Diff line number Diff line change
1
+ ---
2
+ title : v-forのref配列
3
+ badges :
4
+ - breaking
5
+ ---
6
+
7
+ # {{ $frontmatter.title }} <MigrationBadges :badges =" $frontmatter.badges " />
8
+
9
+ Vue 2 では、` v-for ` の中で ` ref ` 属性を記述すると、対応する ` $refs ` プロパティに参照の配列を入れます。この動作は、入れ子になった ` v-for ` がある場合、曖昧で非効率的になります。
10
+
11
+ Vue 3 では、この記述では ` $refs ` に配列が作成されなくなりました。1つのバインディングから複数の参照を取得するには、関数に ` ref ` をバインドします (これは新機能です)。
12
+
13
+ ``` html
14
+ <div v-for =" item in list" :ref =" setItemRef" ></div >
15
+ ```
16
+
17
+ オプション 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
+ コンポジション 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
+ 注意点
66
+
67
+ - ` itemRefs ` は配列である必要はありません。 反復キーで参照できるオブジェクトでも構いません。
68
+
69
+ - これにより、必要に応じて ` itemRefs ` をリアクティブにして監視することもできます。
You can’t perform that action at this time.
0 commit comments