Skip to content

Commit 8bb6c2b

Browse files
Kingwlyyx990803
authored andcommitted
support $on multi event (#4860)
1 parent 0598ab0 commit 8bb6c2b

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

flow/component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ declare interface Component {
4141
$set: (obj: Array<mixed> | Object, key: mixed, val: mixed) => void;
4242
$delete: (obj: Object, key: string) => void;
4343
$watch: (expOrFn: string | Function, cb: Function, options?: Object) => Function;
44-
$on: (event: string, fn: Function) => Component;
44+
$on: (event: string | Array<string>, fn: Function) => Component;
4545
$once: (event: string, fn: Function) => Component;
4646
$off: (event?: string, fn?: Function) => Component;
4747
$emit: (event: string, ...args: Array<mixed>) => Component;

src/core/instance/events.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,19 @@ export function updateComponentListeners (
3838

3939
export function eventsMixin (Vue: Class<Component>) {
4040
const hookRE = /^hook:/
41-
Vue.prototype.$on = function (event: string, fn: Function): Component {
41+
Vue.prototype.$on = function (event: string | Array<string>, fn: Function): Component {
4242
const vm: Component = this
43-
;(vm._events[event] || (vm._events[event] = [])).push(fn)
44-
// optimize hook:event cost by using a boolean flag marked at registration
45-
// instead of a hash lookup
46-
if (hookRE.test(event)) {
47-
vm._hasHookEvent = true
43+
if (Array.isArray(event)) {
44+
for (let i = 0, l = event.length; i < l; i++) {
45+
this.$on(event[i], fn)
46+
}
47+
} else {
48+
(vm._events[event] || (vm._events[event] = [])).push(fn)
49+
// optimize hook:event cost by using a boolean flag marked at registration
50+
// instead of a hash lookup
51+
if (hookRE.test(event)) {
52+
vm._hasHookEvent = true
53+
}
4854
}
4955
return vm
5056
}

test/unit/features/instance/methods-events.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ describe('Instance methods events', () => {
1818
expect(spy).toHaveBeenCalledWith(1, 2, 3, 4)
1919
})
2020

21+
it('$on multi event', () => {
22+
vm.$on(['test1', 'test2'], function () {
23+
expect(this).toBe(vm)
24+
spy.apply(this, arguments)
25+
})
26+
vm.$emit('test1', 1, 2, 3, 4)
27+
expect(spy.calls.count()).toBe(1)
28+
expect(spy).toHaveBeenCalledWith(1, 2, 3, 4)
29+
vm.$emit('test2', 5, 6, 7, 8)
30+
expect(spy.calls.count()).toBe(2)
31+
expect(spy).toHaveBeenCalledWith(5, 6, 7, 8)
32+
})
33+
2134
it('$once', () => {
2235
vm.$once('test', spy)
2336
vm.$emit('test', 1, 2, 3)

types/vue.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export declare class Vue {
5959
callback: WatchHandler<this, T>,
6060
options?: WatchOptions
6161
): (() => void);
62-
$on(event: string, callback: Function): this;
62+
$on(event: string | string[], callback: Function): this;
6363
$once(event: string, callback: Function): this;
6464
$off(event?: string, callback?: Function): this;
6565
$emit(event: string, ...args: any[]): this;

0 commit comments

Comments
 (0)