请确保云函数已正确开发并部署。
- import { cloudFunction } from '@kit.CloudFoundationKit'
- import { BusinessError } from '@kit.BasicServicesKit';
- //填入需要调用的云函数名称
- cloudFunction.call({name: 'xxxx'})
- .then((res: cloudFunction.FunctionResult) => {
- // 处理调用返回
- }).catch((err: BusinessError) => {
- // 调用云函数异常时的处理逻辑
- })
例如,调用云侧函数“my-cloud-function”,以返回一个时间戳。
- import { cloudFunction } from '@kit.CloudFoundationKit';
- import { BusinessError } from '@kit.BasicServicesKit';
-
- interface result {
- intervalTime: number
- }
-
- interface res {
- result: result
- }
-
- @Entry
- @Component
- struct CloudFunction {
- @State globalId: string = '';
-
- build() {
- Column() {
- Navigation()
- .title($r('app.string.cloud_function_title'))
- .height('50vp')
- .width('100%')
- .margin({ bottom: 10 })
- .titleMode(NavigationTitleMode.Mini)
-
- Text($r('app.string.cloud_function_description'))
- .width('90%')
- .textAlign(TextAlign.Center)
- .margin({ top: 20, bottom: 20 })
- .fontSize($r('app.float.body_font_size'))
- Button({ type: ButtonType.Normal }) {
- Text($r('app.string.cloud_function_button_text'))
- .fontColor($r('app.color.white'))
- .margin({ top: 5, bottom: 5 })
- }
- .width('90%')
- .borderRadius('8vp')
- .height('30vp')
- .margin({ top: 10 })
- .onClick(() => {
- this.callMyFunction()
- })
-
- Column() {
- Text(this.globalId).fontSize($r('app.float.body_font_size'))
- }
- .width('90%')
- .padding({ top: 20, bottom: 20 })
- .margin({ top: 20 })
- .backgroundColor($r('app.color.placeholder_background'))
- }.height('100%')
- }
-
- callMyFunction() {
- cloudFunction.call({ name: 'my-cloud-function' }).then((res: cloudFunction.FunctionResult) => {
- let callback = res as res;
- console.info(`Succeeded in call the function, time:${callback.result.intervalTime} `);
- }).catch((err: BusinessError) => {
- console.error(`Failed to call the function, Code: ${err.code}, message: ${err.message}`);
- });
- }
- }
合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功