提交 21adee5c 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

feat(event-bus): 补充对象字面量参数示例

上级 b4d1f84e
......@@ -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)
})
})
......@@ -5,7 +5,7 @@
<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>
......@@ -14,6 +14,12 @@
<view>
<view v-for="(item, index) in log" :key="index">{{ item }}</view>
</view>
<button @click="onObj">开始监听 obj 参数</button>
<button @click="emitWithObj">触发监听 obj 参数</button>
<view class="box">
<text>接收到的 obj 参数:</text>
<text>{{JSON.stringify(objArg)}}</text>
</view>
</view>
</view>
<!-- #ifdef APP -->
......@@ -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;
}
</style>
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册