Skip to content

Commit 254d85c

Browse files
javoskiyyx990803
authored andcommitted
fix(ref): refactor function registerRef (#6039)
fix #5997
1 parent d8d4ca6 commit 254d85c

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

Diff for: .flowconfig

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ module.name_mapper='^weex/\(.*\)$' -> '<PROJECT_ROOT>/src/platforms/weex/\1'
2020
module.name_mapper='^server/\(.*\)$' -> '<PROJECT_ROOT>/src/server/\1'
2121
module.name_mapper='^entries/\(.*\)$' -> '<PROJECT_ROOT>/src/entries/\1'
2222
module.name_mapper='^sfc/\(.*\)$' -> '<PROJECT_ROOT>/src/sfc/\1'
23+
suppress_comment= \\(.\\|\n\\)*\\$flow-disable-line

Diff for: src/core/vdom/modules/ref.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ export function registerRef (vnode: VNodeWithData, isRemoval: ?boolean) {
3232
}
3333
} else {
3434
if (vnode.data.refInFor) {
35-
if (Array.isArray(refs[key]) && refs[key].indexOf(ref) < 0) {
36-
refs[key].push(ref)
37-
} else {
35+
if (!Array.isArray(refs[key])) {
3836
refs[key] = [ref]
37+
} else if (refs[key].indexOf(ref) < 0) {
38+
// $flow-disable-line
39+
refs[key].push(ref)
3940
}
4041
} else {
4142
refs[key] = ref

Diff for: test/unit/features/ref.spec.js

+50
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,56 @@ describe('ref', () => {
146146
}
147147
})
148148

149+
it('should work with v-for on dynamic component', done => {
150+
components.test3 = {
151+
id: 'test3',
152+
template: `<test1 v-if="!normal"></test1><div v-else>test3</div>`,
153+
data () {
154+
return { normal: false }
155+
},
156+
components: { test1: components.test }
157+
}
158+
// a flag that representing whether to test component content or not
159+
let testContent = false
160+
161+
const vm = new Vue({
162+
template: `
163+
<div>
164+
<component
165+
v-for="(item, index) in items"
166+
:key="index"
167+
:is="item"
168+
ref="children">
169+
</component>
170+
</div>
171+
`,
172+
data: {
173+
items: ['test2', 'test3']
174+
},
175+
components
176+
}).$mount()
177+
assertRefs()
178+
expect(vm.$refs.children[0].$el.textContent).toBe('test2')
179+
expect(vm.$refs.children[1].$el.textContent).toBe('test')
180+
// updating
181+
vm.$refs.children[1].normal = true
182+
testContent = true
183+
waitForUpdate(assertRefs)
184+
.then(() => { vm.items.push('test') })
185+
.then(assertRefs)
186+
.then(done)
187+
188+
function assertRefs () {
189+
expect(Array.isArray(vm.$refs.children)).toBe(true)
190+
expect(vm.$refs.children.length).toBe(vm.items.length)
191+
if (testContent) {
192+
expect(
193+
vm.$refs.children.every((comp, i) => comp.$el.textContent === vm.items[i])
194+
).toBe(true)
195+
}
196+
}
197+
})
198+
149199
it('should register on component with empty roots', () => {
150200
const vm = new Vue({
151201
template: '<child ref="test"></child>',

0 commit comments

Comments
 (0)