Skip to content

Commit e6c5f21

Browse files
committed
fix event initialization on reused slot nodes (fix #3518)
1 parent 0674509 commit e6c5f21

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

Diff for: src/core/vdom/helpers.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,13 @@ export function updateListeners (
9292
if (Array.isArray(cur)) {
9393
add(event, (cur.invoker = arrInvoker(cur)), capture)
9494
} else {
95-
fn = cur
96-
cur = on[name] = {}
97-
cur.fn = fn
98-
add(event, (cur.invoker = fnInvoker(cur)), capture)
95+
if (!cur.invoker) {
96+
fn = cur
97+
cur = on[name] = {}
98+
cur.fn = fn
99+
cur.invoker = fnInvoker(cur)
100+
}
101+
add(event, cur.invoker, capture)
99102
}
100103
} else if (Array.isArray(old)) {
101104
old.length = cur.length
@@ -126,6 +129,7 @@ function arrInvoker (arr: Array<Function>): Function {
126129
function fnInvoker (o: { fn: Function }): Function {
127130
return function (ev) {
128131
const single = arguments.length === 1
132+
if (typeof o.fn !== 'function') debugger
129133
single ? o.fn(ev) : o.fn.apply(null, arguments)
130134
}
131135
}

Diff for: test/unit/features/component/component-slot.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,32 @@ describe('Component slot', () => {
481481
}).$mount()
482482
expect('Static <slot> found inside v-for').toHaveBeenWarned()
483483
})
484+
485+
// #3518
486+
fit('events should not break when slot is toggled by v-if', done => {
487+
const spy = jasmine.createSpy()
488+
const vm = new Vue({
489+
template: `<test><div class="click" @click="test">hi</div></test>`,
490+
methods: {
491+
test: spy
492+
},
493+
components: {
494+
test: {
495+
data: () => ({
496+
toggle: true
497+
}),
498+
template: `<div v-if="toggle"><slot></slot></div>`
499+
}
500+
}
501+
}).$mount()
502+
503+
expect(vm.$el.textContent).toBe('hi')
504+
vm.$children[0].toggle = false
505+
waitForUpdate(() => {
506+
vm.$children[0].toggle = true
507+
}).then(() => {
508+
triggerEvent(vm.$el.querySelector('.click'), 'click')
509+
expect(spy).toHaveBeenCalled()
510+
}).then(done)
511+
})
484512
})

0 commit comments

Comments
 (0)