From 21adee5cc21c15b33531e7266998c4f4e8a106ac Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Wed, 31 Jul 2024 11:38:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(event-bus):=20=E8=A1=A5=E5=85=85=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E5=AD=97=E9=9D=A2=E9=87=8F=E5=8F=82=E6=95=B0=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/API/event-bus/event-bus.test.js | 44 ++++++++++++++++----------- pages/API/event-bus/event-bus.uvue | 37 +++++++++++++++------- 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/pages/API/event-bus/event-bus.test.js b/pages/API/event-bus/event-bus.test.js index bf58ee9d..f3aa542d 100644 --- a/pages/API/event-bus/event-bus.test.js +++ b/pages/API/event-bus/event-bus.test.js @@ -50,23 +50,31 @@ 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) + }) + + it('emit object params', async () => { + await page.callMethod('onObj') + await page.callMethod('emitWithObj') + const objArg = await page.data('objArg') + expect(objArg.a).toBe(1) + expect(objArg.b).toBe(2) + }) + + 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 a34651a7..274f7c1f 100644 --- a/pages/API/event-bus/event-bus.uvue +++ b/pages/API/event-bus/event-bus.uvue @@ -5,7 +5,7 @@ - + @@ -14,6 +14,12 @@ {{ item }} + + + + 接收到的 obj 参数: + {{JSON.stringify(objArg)}} + @@ -26,33 +32,42 @@ data() { return { log: [] as string[], + objArg: {}, } }, methods: { fn(res : string) { this.log.push(res) - }, - fn2(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) + }, + on2() { + uni.$on('test', this.fn2) + }, + onObj() { + uni.$on('test-obj', (res: UTSJSONObject) => { + this.objArg = res + }) }, once() { uni.$once('test', this.fn) }, off() { uni.$off('test', this.fn) - }, - offAll() { - uni.$off('test') + }, + offAll() { + uni.$off('test') }, emit() { uni.$emit('test', 'msg:' + Date.now()) }, + emitWithObj() { + uni.$emit('test-obj', { a: 1, b: 2 }) + }, clear() { this.log.length = 0 }, @@ -64,4 +79,4 @@ .box { padding: 10px; } - + -- GitLab