From 1f76df97004be0e3e63a31573f7df9ce217a21a1 Mon Sep 17 00:00:00 2001 From: wangyaqi Date: Wed, 17 Apr 2024 17:40:50 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E6=9B=B4=E6=96=B0uni-app-x=20uniCloud?= =?UTF-8?q?=E4=BA=91=E5=87=BD=E6=95=B0=E4=BA=91=E5=AF=B9=E8=B1=A1=E6=B3=9B?= =?UTF-8?q?=E5=9E=8B=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api/unicloud/function.md | 43 +++++++++++++++++++++++++++++++++++ docs/api/unicloud/object.md | 42 +++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/docs/api/unicloud/function.md b/docs/api/unicloud/function.md index 686c0ff1..c99ad534 100644 --- a/docs/api/unicloud/function.md +++ b/docs/api/unicloud/function.md @@ -11,3 +11,46 @@ + +### 调用云函数时传入泛型 + +> 4.13版本起支持,仅安卓端会构造对应的泛型的实例,web端和iOS端泛型仅作为类型使用。 + +用法:`uniCloud.callFunction<泛型类型>({name: 'xxx', data: {}} as UniCloudCallFunctionOptions)` + +在不传泛型时callFunction返回的类型为`Promise>`,传入泛型后callFunction返回的类型为`Promise>` + +**代码示例** + +```ts +// 云函数fn代码 +'use strict'; +exports.main = async (event, context) => { + return { + errCode: 0, + errMsg: '', + detail: 'call function detail' + }; +}; +``` + +```ts +// 客户端代码 +type CallFunctionResult = { + errCode : number, + errMsg : string, + detail : string +} +uniCloud.callFunction( + { + name: 'fn', + data: { a: 1 } as UTSJSONObject, + } as UniCloudCallFunctionOptions +).then(function (res) { + const result = res.result // result的类型为CallFunctionResult + const detail = result.detail // 可直接使用.detail访问 + console.log(detail) +}).catch(function (err : any | null) { + console.error(err) +}) +``` diff --git a/docs/api/unicloud/object.md b/docs/api/unicloud/object.md index 91ba78f5..25532a46 100644 --- a/docs/api/unicloud/object.md +++ b/docs/api/unicloud/object.md @@ -32,4 +32,44 @@ export { // 上面的写法可以自己调整,仅需保证export内包含所 } ``` - \ No newline at end of file + + +### 调用云对象时传入泛型 + +> 4.13版本起支持,仅安卓端会构造对应的泛型的实例,web端和iOS端泛型仅作为类型使用。 + +用法:`obj.add<泛型类型>()` + +在不传泛型时云对象方法返回的类型为`Promise`,传入泛型后callFunction返回的类型为`Promise<泛型类型>` + +**代码示例** + +```ts +// 云对象todo代码 +'use strict'; +module.exports = { + async add(title, content) { + return { + errCode: 0, + errMsg: '', + detail: `Todo added, title: ${title}, content: ${content}` + } + }, +} +``` + +```ts +// 客户端代码 +const todo = uniCloud.importObject('todo') +type CallObjectResult = { + errCode : number + errMsg : string + detail : string +} +todo.add('todo title', 'todo content').then((res) => { + const detail = res.detail // res类型为CallObjectResult,可直接通过.detail访问其中detail属性 + console.log(detail) +}).catch((err : any | null) => { + console.error(err) +}) +``` \ No newline at end of file -- GitLab