From 26316aa5f7f2b38c747851f5b0b8ff98595bb7d9 Mon Sep 17 00:00:00 2001 From: hdx Date: Mon, 8 Apr 2024 17:05:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(uni.event):=20=E6=96=B0=E5=A2=9E=20uni.$of?= =?UTF-8?q?f=20=E7=9A=84=E7=AC=AC=E4=BA=8C=E4=B8=AA=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E4=B8=BA=E5=8F=AF=E9=80=89=EF=BC=8C=E4=B8=8D=E4=BC=A0=E9=80=92?= =?UTF-8?q?=E6=97=B6=E7=A7=BB=E9=99=A4=E6=89=80=E6=9C=89=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/API/event-bus/event-bus.test.js | 18 ++++++++++++++++++ pages/API/event-bus/event-bus.uvue | 13 ++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pages/API/event-bus/event-bus.test.js b/pages/API/event-bus/event-bus.test.js index 699466de..bf58ee9d 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 fa818297..30c65d63 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()) -- GitLab