# importObject(objectName, options?)

引用云对象

# 参数

名称 类型 必填 默认值 描述
objectName string (string.CloudObjectString) - -
options UniCloudImportObjectOptions - -
名称 类型 必备 默认值 描述
customUI boolean | null - 是否移除自动展示的ui
loadingOptions UniCloudImportObjectLoadingOptions | null - loading界面配置
名称 类型 必备 默认值 描述
title string | null - 加载框标题
mask boolean | null - 加载框是否显示mask
errorOptions UniCloudImportObjectErrorOptions | null - 错误提示配置
名称 类型 必备 默认值 描述
type string | null - 错误提示类型,可以是modal或者toast
retry boolean | null - 是否显示重试按钮

# 返回值

类型
UniCloudCloudObjectCaller
名称 类型 必备 默认值 描述
_obj InternalUniCloudCloudObject - -
# InternalUniCloudCloudObject 的方法
# callMethod(methodName, args)
# 参数
名称 类型 必填 默认值 描述
methodName string - -
args Array<any | null> - -
# 返回值
类型
Promise<T>

# UniCloudCloudObjectCaller 的方法

# _getArgs(args?)

# 参数
名称 类型 必填 默认值 描述
args Array<any | null> - -
# 返回值
类型
Array<any | null>

# importObject 兼容性

Android iOS web
阿里云 3.9,腾讯云 3.91,支付宝云 3.98 4.11 4.0

# 参见

相关 Bug

# uni-app x内使用云对象的特殊说明

由于强类型语言的限制,uni-app-x在编译时需要读取本地云对象导出的方法列表生成客户端对象。

  • 请确保调用的云对象在本地包含导出的方法。
  • 请确保本地工程的云对象的方法是正确的。
  • 如由于云函数加密等因素导致index.obj.js无法被正确解析,请在云对象目录创建index.obj.d.ts声明云对象内包含的方法。index.obj.d.ts示例代码如下:
// interface.d.ts
type AnyFunction = (...args: any[]) => any;

declare const add: AnyFunction
declare const update: AnyFunction
declare const deleteRecord: AnyFunction

export { // 上面的写法可以自己调整,仅需保证export内包含所有方法即可
  add,
  update
  deleteRecord as remove
}

# 示例

hello uni-app x

Template

Script

<template>
  <!-- #ifdef APP -->
  <scroll-view class="page-scroll-view">
  <!-- #endif -->
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="addTodo">添加Todo</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="addTodoWithGeneric">添加Todo传入泛型</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="randomFail">随机触发失败重试</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="fail">云对象失败调用</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="failWithNumberErrCode">云对象数字错误码</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="success">云对象成功调用</button>
        </view>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>



<style>

</style>

# 调用云对象时传入泛型

4.13版本起支持,仅安卓端会构造对应的泛型的实例,web端和iOS端泛型仅作为类型使用。

用法:obj.add<泛型类型>()

在不传泛型时云对象方法返回的类型为Promise<UTSJSONObject>,传入泛型后callFunction返回的类型为Promise<泛型类型>

代码示例

// 云对象todo代码
'use strict';
module.exports = {
  async add(title, content) {
    return {
      errCode: 0,
      errMsg: '',
      detail: `Todo added, title: ${title}, content: ${content}`
    }
  },
}
// 客户端代码
const todo = uniCloud.importObject('todo')
type CallObjectResult = {
  errCode : number
  errMsg : string
  detail : string
}
todo.add<CallObjectResult>('todo title', 'todo content').then((res) => {
  const detail = res.detail // res类型为CallObjectResult,可直接通过.detail访问其中detail属性
  console.log(detail)
}).catch((err : any | null) => {
  console.error(err)
})