Skip to content

Commit 920ddc2

Browse files
committed
only set a key to VNode have similar shape, fix #5618
1 parent b977c77 commit 920ddc2

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/core/vdom/helpers/normalize-children.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import VNode, { createTextVNode } from 'core/vdom/vnode'
4-
import { isDef, isUndef, isPrimitive } from 'shared/util'
4+
import { isDef, isUndef, isPrimitive, isObject } from 'shared/util'
55

66
// The template compiler attempts to minimize the need for normalization by
77
// statically analyzing the template at compile time.
@@ -45,7 +45,11 @@ function normalizeArrayChildren (children: any, nestedIndex?: string): Array<VNo
4545
last = res[res.length - 1]
4646
// nested
4747
if (Array.isArray(c)) {
48-
res.push.apply(res, normalizeArrayChildren(c, `${nestedIndex || ''}_${i}`))
48+
if (hasNestedIndex(c)) {
49+
res.push.apply(res, normalizeArrayChildren(c, `${nestedIndex || ''}_${i}`))
50+
} else {
51+
res.push.apply(res, normalizeArrayChildren(c))
52+
}
4953
} else if (isPrimitive(c)) {
5054
if (isDef(last) && isDef(last.text)) {
5155
last.text += String(c)
@@ -67,3 +71,21 @@ function normalizeArrayChildren (children: any, nestedIndex?: string): Array<VNo
6771
}
6872
return res
6973
}
74+
75+
function hasNestedIndex (children: any): boolean {
76+
const length = children.length
77+
if (length <= 1) return true
78+
if (isObject(children[0]) === false || isUndef(children[0].tag) || isDef(children[0].key)) return false
79+
let i
80+
for (i = 1; i < length; i++) {
81+
if (isObject(children[i]) === false || similarVNode(children[0], children[i]) === false) return false
82+
}
83+
return true
84+
}
85+
86+
function similarVNode (a: VNode, b: VNode): boolean {
87+
return (
88+
a.tag === b.tag &&
89+
a.key === b.key
90+
)
91+
}

0 commit comments

Comments
 (0)