Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
unidocs-zh
提交
4e9c8bf5
unidocs-zh
项目概览
DCloud
/
unidocs-zh
通知
3335
Star
107
Fork
850
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
102
列表
看板
标记
里程碑
合并请求
84
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
unidocs-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
102
Issue
102
列表
看板
标记
里程碑
合并请求
84
合并请求
84
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
4e9c8bf5
编写于
10月 07, 2023
作者:
H
hdx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat($callMethod): 增加使用说明和示例代码
上级
d8a95ab0
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
88 addition
and
0 deletion
+88
-0
docs/uni-app-x/tutorial/codegap.md
docs/uni-app-x/tutorial/codegap.md
+88
-0
未找到文件。
docs/uni-app-x/tutorial/codegap.md
浏览文件 @
4e9c8bf5
...
...
@@ -272,3 +272,91 @@ request({url: 'https://www.example.com/request'} as RequestOptions)
## css使用注意
[
详见
](
uni-app-x/css/README.md
)
## 如何调用组件方法
使用
`this.$refs`
获取组件实例,通过
`$callMethod`
调用组件方法
`$callMethod`
支持多个参数
页面示例代码
`page1.uvue`
```
html
<template>
<view>
<component1
ref=
"component1"
></component1>
</view>
</template>
<script>
// 导入 vue 组件实例类型
import
{
ComponentPublicInstance
}
from
'
vue
'
// 引用组件 component1.uvue, 如果使用 easycom 可省略此步骤
import
component1
from
'
./component1.uvue
'
export
default
{
components
:
{
component1
},
data
()
{
return
{
}
},
onReady
()
{
// 获取组件 ref 属性 component1
const
component1
=
this
.
$refs
[
'
component1
'
]
!
as
ComponentPublicInstance
;
// 通过 $callMethod 调用组件的 foo1 方法
component1
.
$callMethod
(
'
foo1
'
);
// 通过 $callMethod 调用组件的 foo2 方法并传递 1个参数
component1
.
$callMethod
(
'
foo2
'
,
Date
.
now
());
// 通过 $callMethod 调用组件的 foo3 方法并传递 2个参数
component1
.
$callMethod
(
'
foo3
'
,
Date
.
now
(),
Date
.
now
());
// 通过 $callMethod 调用组件的 foo4 方法并传递 callback
component1
.
$callMethod
(
'
foo4
'
,
()
=>
{
console
.
log
(
'
callback
'
)
});
// 通过 $callMethod 调用组件的 foo5 方法并接收返回值
// 注意: 返回值可能为 null,当前例子一定不为空,所以加了 !
const
result
=
component1
.
$callMethod
(
'
foo5
'
,
'
string1
'
)
!
as
string
;
console
.
log
(
result
);
// string1
}
}
</script>
```
组件示例代码
`component1.uvue`
```
html
<template>
<view></view>
</template>
<script>
export
default
{
data
()
{
return
{
}
},
methods
:
{
foo1
()
{
},
foo2
(
date1
:
number
)
{
},
foo3
(
date1
:
number
,
date2
:
number
)
{
},
foo4
(
callback
:
(()
=>
void
))
{
callback
()
},
foo5
(
text1
:
string
)
:
any
|
null
{
return
text1
}
}
}
</script>
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录