Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
deea2ccb
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
5992
Star
90
Fork
162
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
18
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
18
Issue
18
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
deea2ccb
编写于
11月 05, 2024
作者:
DCloud-WZF
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 补充 $emit 无参及多参数情况示例及测试 issue:11588
上级
cfe12ceb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
107 addition
and
74 deletion
+107
-74
pages/API/event-bus/event-bus.test.js
pages/API/event-bus/event-bus.test.js
+17
-0
pages/API/event-bus/event-bus.uvue
pages/API/event-bus/event-bus.uvue
+90
-74
未找到文件。
pages/API/event-bus/event-bus.test.js
浏览文件 @
deea2ccb
...
...
@@ -86,4 +86,21 @@ describe('event-bus', () => {
expect
(
logs
[
0
]).
toBe
(
'
触发 test-return-id $on fn
'
)
expect
(
logs
[
1
]).
toBe
(
'
触发 test-return-id $once fn
'
)
})
it
(
'
test $emit no args
'
,
async
()
=>
{
await
page
.
callMethod
(
'
clear
'
)
expect
((
await
page
.
data
()).
log
.
length
).
toBe
(
0
)
await
page
.
callMethod
(
'
testEmitNoArgs
'
)
const
logs
=
await
page
.
data
(
'
log
'
)
expect
(
logs
.
length
).
toBe
(
1
)
expect
(
logs
[
0
]).
toBe
(
'
test-emit-no-args
'
)
})
it
(
'
test $emit multiple args
'
,
async
()
=>
{
await
page
.
callMethod
(
'
clear
'
)
expect
((
await
page
.
data
()).
log
.
length
).
toBe
(
0
)
await
page
.
callMethod
(
'
testEmitMultipleArgs
'
)
const
logs
=
await
page
.
data
(
'
log
'
)
expect
(
logs
.
length
).
toBe
(
1
)
expect
(
logs
[
0
]).
toBe
(
'
arg1_2
'
)
})
})
pages/API/event-bus/event-bus.uvue
浏览文件 @
deea2ccb
<template>
<!-- #ifdef APP -->
<scroll-view class="page-scroll-view">
<!-- #endif -->
<view>
<button @click="on">开始监听</button>
<button @click="once">监听一次</button>
<button @click="off">取消监听</button>
<!-- #endif -->
<view
class="box"
>
<button
class="uni-btn"
@click="on">开始监听</button>
<button
class="uni-btn"
@click="once">监听一次</button>
<button
class="uni-btn"
@click="off">取消监听</button>
<!-- <button @click="offAll">取消全部监听</button> -->
<button @click="emit">触发监听</button>
<button @click="clear">清空消息</button>
<view
class="box"
>
<view>收到的消息:</view>
<view>
<button
class="uni-btn"
@click="emit">触发监听</button>
<button
class="uni-btn"
@click="clear">清空消息</button>
<view>
<view
class="uni-btn"
>收到的消息:</view>
<view
class="uni-btn"
>
<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"
>
<button
class="uni-btn"
@click="onObj">开始监听 obj 参数</button>
<button
class="uni-btn"
@click="emitWithObj">触发监听 obj 参数</button>
<view class=
'uni-btn'
>
<text>接收到的 obj 参数:</text>
<text>{{
JSON.stringify(objArg)
}}</text>
<text>{{
JSON.stringify(objArg)
}}</text>
</view>
<button @click="testReturnId">测试返回 id</button>
<button class='uni-btn' @click="testReturnId">测试返回 id</button>
<button class='uni-btn' @click="testEmitNoArgs">测试 $emit 无参</button>
<button class='uni-btn' @click="testEmitMultipleArgs">测试 $emit 多个参数</button>
</view>
</view>
<!-- #ifdef APP -->
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script>
export default {
data() {
return {
log: [] as string[],
objArg: {},
}
export default {
data() {
return {
log: [] as string[],
objArg: {},
}
},
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)
},
onObj() {
uni.$on('test-obj', (res : UTSJSONObject) => {
this.objArg = res
})
},
once() {
uni.$once('test', this.fn)
},
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)
},
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')
},
emit() {
uni.$emit('test', 'msg:' + Date.now())
},
emitWithObj() {
uni.$emit('test-obj', { a: 1, b: 2 })
},
clear() {
this.log.length = 0
},
testReturnId(){
const id1 = uni.$on('test-return-id', this.fn)
uni.$emit('test-return-id', '触发 test-return-id $on fn')
uni.$off('test-return-id', id1)
uni.$emit('test-return-id', '触发 test-return-id $on fn')
off() {
uni.$off('test', this.fn)
},
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
},
testReturnId(){
const id1 = uni.$on('test-return-id', this.fn)
uni.$emit('test-return-id', '触发 test-return-id $on fn')
uni.$off('test-return-id', id1)
uni.$emit('test-return-id', '触发 test-return-id $on fn')
uni.$once('test-return-id', this.fn)
uni.$emit('test-return-id', '触发 test-return-id $once fn')
uni.$emit('test-return-id', '触发 test-return-id $once fn')
const id2 = uni.$once('test-id', this.fn)
uni.$off('test-return-id', id2)
uni.$emit('test-return-id', '触发 test-return-id $once fn')
}
uni.$once('test-return-id', this.fn)
uni.$emit('test-return-id', '触发 test-return-id $once fn')
uni.$emit('test-return-id', '触发 test-return-id $once fn')
const id2 = uni.$once('test-id', this.fn)
uni.$off('test-return-id', id2)
uni.$emit('test-return-id', '触发 test-return-id $once fn')
},
testEmitNoArgs() {
uni.$on('test-emit-no-args', () => {
this.log.push('test-emit-no-args')
})
uni.$emit('test-emit-no-args')
uni.$off('test-emit-no-args')
},
}
testEmitMultipleArgs() {
uni.$on('test-emit-multiple-args', (arg1 : string, arg2 : number) => {
this.log.push(`${arg1}_${arg2}`)
})
uni.$emit('test-emit-multiple-args', 'arg1', 2)
uni.$off('test-emit-multiple-args')
}
},
}
</script>
<style>
.box {
padding: 10px;
}
.box {
padding: 10px;
}
</style>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录