diff --git a/pages/API/event-bus/event-bus.test.js b/pages/API/event-bus/event-bus.test.js index 699466dea86f00260d57c933d260540b3d3d09fa..bf58ee9d91079f3799bd60d6a841817e2524730a 100644 --- a/pages/API/event-bus/event-bus.test.js +++ b/pages/API/event-bus/event-bus.test.js @@ -50,5 +50,23 @@ describe('event-bus', () => { await page.callMethod('emit') const l3 = (await page.data()).log.length expect(l3).toBe(0) + }) + + it('off-all', async () => { + await page.callMethod('clear') + await page.callMethod('on') + await page.callMethod('on2') + await page.callMethod('emit') + const l1 = (await page.data()).log.length + expect(l1).toBe(2) + + await page.callMethod('clear') + const l2 = (await page.data()).log.length + expect(l2).toBe(0) + + await page.callMethod('offAll') + await page.callMethod('emit') + const l3 = (await page.data()).log.length + expect(l3).toBe(0) }) }) diff --git a/pages/API/event-bus/event-bus.uvue b/pages/API/event-bus/event-bus.uvue index fa81829718ab821b4967a4f987b7450b004bb599..30c65d637f0194e7690e5c8015ee865d92ecbef7 100644 --- a/pages/API/event-bus/event-bus.uvue +++ b/pages/API/event-bus/event-bus.uvue @@ -5,7 +5,8 @@ - + + @@ -30,15 +31,25 @@ methods: { fn(res : string) { this.log.push(res) + }, + fn2(res : string) { + this.log.push(res) }, on() { uni.$on('test', this.fn) + }, + on2() { + uni.$on('test', this.fn2) }, once() { uni.$once('test', this.fn) }, off() { uni.$off('test', this.fn) + }, + offAll() { + // TODO 第二个参数为可选,后续优化后可不传递 + uni.$off('test', null) }, emit() { uni.$emit('test', 'msg:' + Date.now())