diff --git a/pages/API/event-bus/event-bus.test.js b/pages/API/event-bus/event-bus.test.js index bf58ee9d91079f3799bd60d6a841817e2524730a..f3aa542d21176af817e96614f99e0698dfacf540 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 a34651a7515916e25f6a8f7027bdf76bed7affd3..274f7c1f54a297ed240409074453218add1962de 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; } - +