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

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

上级 b4d1f84e
...@@ -50,23 +50,31 @@ describe('event-bus', () => { ...@@ -50,23 +50,31 @@ describe('event-bus', () => {
await page.callMethod('emit') await page.callMethod('emit')
const l3 = (await page.data()).log.length const l3 = (await page.data()).log.length
expect(l3).toBe(0) expect(l3).toBe(0)
}) })
it('off-all', async () => { it('emit object params', async () => {
await page.callMethod('clear') await page.callMethod('onObj')
await page.callMethod('on') await page.callMethod('emitWithObj')
await page.callMethod('on2') const objArg = await page.data('objArg')
await page.callMethod('emit') expect(objArg.a).toBe(1)
const l1 = (await page.data()).log.length expect(objArg.b).toBe(2)
expect(l1).toBe(2) })
await page.callMethod('clear') it('off-all', async () => {
const l2 = (await page.data()).log.length await page.callMethod('clear')
expect(l2).toBe(0) await page.callMethod('on')
await page.callMethod('on2')
await page.callMethod('offAll') await page.callMethod('emit')
await page.callMethod('emit') const l1 = (await page.data()).log.length
const l3 = (await page.data()).log.length expect(l1).toBe(2)
expect(l3).toBe(0)
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 @@ ...@@ -5,7 +5,7 @@
<view> <view>
<button @click="on">开始监听</button> <button @click="on">开始监听</button>
<button @click="once">监听一次</button> <button @click="once">监听一次</button>
<button @click="off">取消监听</button> <button @click="off">取消监听</button>
<!-- <button @click="offAll">取消全部监听</button> --> <!-- <button @click="offAll">取消全部监听</button> -->
<button @click="emit">触发监听</button> <button @click="emit">触发监听</button>
<button @click="clear">清空消息</button> <button @click="clear">清空消息</button>
...@@ -14,6 +14,12 @@ ...@@ -14,6 +14,12 @@
<view> <view>
<view v-for="(item, index) in log" :key="index">{{ item }}</view> <view v-for="(item, index) in log" :key="index">{{ item }}</view>
</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>
</view> </view>
<!-- #ifdef APP --> <!-- #ifdef APP -->
...@@ -26,33 +32,42 @@ ...@@ -26,33 +32,42 @@
data() { data() {
return { return {
log: [] as string[], log: [] as string[],
objArg: {},
} }
}, },
methods: { methods: {
fn(res : string) { fn(res : string) {
this.log.push(res) this.log.push(res)
}, },
fn2(res : string) { fn2(res : string) {
this.log.push(res) this.log.push(res)
}, },
on() { on() {
uni.$on('test', this.fn) uni.$on('test', this.fn)
}, },
on2() { on2() {
uni.$on('test', this.fn2) uni.$on('test', this.fn2)
},
onObj() {
uni.$on('test-obj', (res: UTSJSONObject) => {
this.objArg = res
})
}, },
once() { once() {
uni.$once('test', this.fn) uni.$once('test', this.fn)
}, },
off() { off() {
uni.$off('test', this.fn) uni.$off('test', this.fn)
}, },
offAll() { offAll() {
uni.$off('test') uni.$off('test')
}, },
emit() { emit() {
uni.$emit('test', 'msg:' + Date.now()) uni.$emit('test', 'msg:' + Date.now())
}, },
emitWithObj() {
uni.$emit('test-obj', { a: 1, b: 2 })
},
clear() { clear() {
this.log.length = 0 this.log.length = 0
}, },
...@@ -64,4 +79,4 @@ ...@@ -64,4 +79,4 @@
.box { .box {
padding: 10px; padding: 10px;
} }
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册