Skip to content

Commit 2731452

Browse files
committed
v-if: factory creation should be lazy
1 parent b3d8e37 commit 2731452

File tree

1 file changed

+12
-3
lines changed
  • src/directives/public

1 file changed

+12
-3
lines changed

src/directives/public/if.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ export default {
1919
var next = el.nextElementSibling
2020
if (next && getAttr(next, 'v-else') !== null) {
2121
remove(next)
22-
this.elseFactory = new FragmentFactory(next._context || this.vm, next)
22+
this.elseEl = next
2323
}
2424
// check main block
2525
this.anchor = createAnchor('v-if')
2626
replace(el, this.anchor)
27-
this.factory = new FragmentFactory(this.vm, el)
2827
} else {
2928
process.env.NODE_ENV !== 'production' && warn(
3029
'v-if="' + this.expression + '" cannot be ' +
@@ -50,6 +49,10 @@ export default {
5049
this.elseFrag.remove()
5150
this.elseFrag = null
5251
}
52+
// lazy init factory
53+
if (!this.factory) {
54+
this.factory = new FragmentFactory(this.vm, this.el)
55+
}
5356
this.frag = this.factory.create(this._host, this._scope, this._frag)
5457
this.frag.before(this.anchor)
5558
},
@@ -59,7 +62,13 @@ export default {
5962
this.frag.remove()
6063
this.frag = null
6164
}
62-
if (this.elseFactory && !this.elseFrag) {
65+
if (this.elseEl && !this.elseFrag) {
66+
if (!this.elseFactory) {
67+
this.elseFactory = new FragmentFactory(
68+
this.elseEl._context || this.vm,
69+
this.elseEl
70+
)
71+
}
6372
this.elseFrag = this.elseFactory.create(this._host, this._scope, this._frag)
6473
this.elseFrag.before(this.anchor)
6574
}

0 commit comments

Comments
 (0)