提交 26316aa5 编写于 作者: H hdx

feat(uni.event): 新增 uni.$off 的第二个参数为可选,不传递时移除所有监听

上级 51333e06
......@@ -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)
})
})
......@@ -5,7 +5,8 @@
<view>
<button @click="on">开始监听</button>
<button @click="once">监听一次</button>
<button @click="off">取消监听</button>
<button @click="off">取消监听</button>
<button @click="offAll">取消全部监听</button>
<button @click="emit">触发监听</button>
<button @click="clear">清空消息</button>
<view class="box">
......@@ -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())
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册