小程序API能力汇总——基础容器API(二)

ty.preDownloadMiniApp

预下载智能小程序,此接口仅供提供预下载普通智能小程序调用,面板小程序的预下载需要使用另外的接口。

需引入MiniKit,且在>=2.3.0版本才可使用

参数

Object object

属性 类型 默认值 必填 说明
miniAppId string 小程序 id
miniAppVersion string 指定小程序版本(可选参数)
complete function 接口调用结束的回调函数(调用成功、失败都会执行)
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数

函数定义示例

复制代码
/**
 * 预下载智能小程序,此接口仅供提供预下载普通智能小程序调用,面板小程序的预下载需要使用另外的接口。
 */
export function preDownloadMiniApp(params: {
  /** 小程序id */
  miniAppId: string;
  /** 指定小程序版本(可选参数) */
  miniAppVersion?: string;
  complete?: () => void;
  success?: (params: null) => void;
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

ty.showMenuButton

显示右上角胶囊按钮

需引入MiniKit,且在>=2.5.0版本才可使用

参数

Object object

属性 类型 默认值 必填 说明
complete function 接口调用结束的回调函数(调用成功、失败都会执行)
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数

函数定义示例

复制代码
/**
 * 显示右上角胶囊按钮
 */
export function showMenuButton(params?: {
  complete?: () => void;
  success?: (params: null) => void;
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

ty.hideMenuButton

隐藏右上角胶囊按钮

需引入MiniKit,且在>=2.5.0版本才可使用

参数

Object object

属性 类型 默认值 必填 说明
complete function 接口调用结束的回调函数(调用成功、失败都会执行)
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数

函数定义示例

复制代码
/**
 * 隐藏右上角胶囊按钮
 */
export function hideMenuButton(params?: {
  complete?: () => void;
  success?: (params: null) => void;
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

ty.setPageOrientation

屏幕旋转设置,auto / portrait / landscape。pad 模式下不支持屏幕旋转

需引入MiniKit,且在>=2.4.1版本才可使用

参数

Object object

属性 类型 默认值 必填 说明
pageOrientation string 屏幕旋转设置, auto(暂不支持) / portrait / landscape
complete function 接口调用结束的回调函数(调用成功、失败都会执行)
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数

函数定义示例

复制代码
/**
 * 屏幕旋转设置,auto / portrait / landscape。pad模式下不支持屏幕旋转
 */
export function setPageOrientation(params: {
  /** 屏幕旋转设置, auto(暂不支持) / portrait / landscape */
  pageOrientation: string;
  complete?: () => void;
  success?: (params: null) => void;
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

MiniWidgetDialogTask ty.openMiniWidget

用来控制小部件弹窗显示和关闭

需引入BaseKit,且在>=3.0.3版本才可使用。

参数

Object object

打开小部件弹窗的回调函数

回调参数 Object res

属性 类型 默认值 必填 说明
appId string 要打开的小部件 appid
pagePath string 对应的小部件页面相对 URL,如果为空则打开首页 path 中 ? 后面的部分会成为 query
deviceId string 面板类型设备 id
groupId string 面板群组类型群组 id
style string 小部件样式,默认 middle
versionType WidgetVersionType 版本类型,默认 release
version string 版本号
position WidgetPosition 展示位置,默认 bottom
autoDismiss boolean 点击空白处是否关闭
autoCache boolean 是否优先展示默认缓存,对应属性在小程序容器 3.1.0 生效
supportDark boolean 是否支持深色模式
complete function 接口调用结束的回调函数(调用成功、失败都会执行)
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数

object.fail 回调参数

参数

Object res

属性 类型 说明
errorMsg string 插件错误信息
errorCode string 错误码
innerError object 插件外部依赖错误信息 {errorMsg: string, errorCode: string }

返回值

MiniWidgetDialogTask 用来控制小部件弹窗显示和关闭

函数定义示例

复制代码
/**
 * 用来控制小部件弹窗显示和关闭
 */
interface MiniWidgetDialogTask {
  /**
   * 关闭小部件弹窗
   */
  dismissMiniWidget(params: {
    complete?: () => void;
    success?: (params: null) => void;
    fail?: (params: {
      errorMsg: string;
      errorCode: string | number;
      innerError: {
        errorCode: string | number;
        errorMsg: string;
      };
    }) => void;
  }): void;
 
  /**
   * 监听 widget 关闭事件
   */
  onWidgetDismiss(
    listener: (params: {
      /** widget 弹窗 ID */
      dialogId: string;
    }) => void,
  ): void;
 
  /**
   * 取消监听 widget 关闭事件
   */
  offWidgetDismiss(
    listener: (params: {
      /** widget 弹窗 ID */
      dialogId: string;
    }) => void,
  ): void;
}
export function openMiniWidget(params: {
  /** 要打开的小部件 appid */
  appId: string;
  /** 对应的小部件页面相对 URL,如果为空则打开首页 path 中 ? 后面的部分会成为 query */
  pagePath?: string;
  /** 面板类型设备 ID */
  deviceId?: string;
  /** 面板群组类型群组 ID */
  groupId?: string;
  /** 小部件样式,默认 middle */
  style?: string;
  /** 版本类型,默认 release */
  versionType?: WidgetVersionType;
  /** 版本号 */
  version?: string;
  /** 展示位置,默认 bottom */
  position?: WidgetPosition;
  /** 点击空白处是否关闭 */
  autoDismiss?: boolean;
  /**
   * 是否优先展示默认缓存
   * 对应属性在小程序容器 3.1.0 生效
   */
  autoCache?: boolean;
  /** 是否支持深色模式 */
  supportDark?: boolean;
  complete?: () => void;
  success?: (params: null) => void;
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): MiniWidgetDialogTask;

MiniWidgetDialogTask

MiniWidgetDialogTask.dismissMiniWidget

关闭小部件弹窗

参数

Object object

关闭小部件弹窗的回调函数

回调参数 Object res

属性 类型 默认值 必填 说明
complete function 接口调用结束的回调函数(调用成功、失败都会执行)
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数

函数定义示例

复制代码
/**
 * 关闭小部件弹窗
 */
export function dismissMiniWidget(params: {
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
  complete?: () => void;
  /** 接口调用成功的回调函数 */
  success?: (params: null) => void;
  /** 接口调用失败的回调函数 */
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

MiniWidgetDialogTask.onWidgetDismiss

监听 widget 关闭事件

参数

function callback

监听 widget 关闭事件的回调函数

回调参数 Object res

属性 类型 默认值 必填 说明
dialogId string widget 弹窗 ID

MiniWidgetDialogTask.offWidgetDismiss

取消监听 widget 关闭事件

参数

function callback

取消监听 widget 关闭事件的回调函数

回调参数 Object res

属性 类型 默认值 必填 说明
dialogId string widget 弹窗 ID

👉 立即开发

相关推荐
lichenyang45314 分钟前
Docker 学习笔记(一):为什么需要镜像、容器和仓库?
前端
kyriewen26 分钟前
别再对着 TypeScript 报错发呆了:我把 10 个最常见的红色波浪线翻译成了人话
前端·javascript·typescript
IT_陈寒27 分钟前
SpringBoot自动配置的坑,我的API突然就404了
前端·人工智能·后端
free351 小时前
从 0 实现一个 Tiny JavaScript VM:项目架构拆解
javascript
奇奇怪怪的1 小时前
Embedding 模型 10+ 横向评测
前端
陈广亮1 小时前
Monorepo 从 0 到 1 实操指南 2026 版:pnpm catalogs + Turborepo 2.x + changesets 全链路
前端
子兮曰1 小时前
OpenMontage 深度解剖:你的 AI 编程助手,其实是个视频工作室
前端·后端·ai编程
敲代码的鱼1 小时前
PDF 预览与签名批注写回 支持安卓 iOS 鸿蒙 UTS插件
android·前端·ios
子兮曰2 小时前
前端工具链的「Rust 化」:一场没有赢家的军备竞赛?
前端·后端·rust
Hyyy3 小时前
Function Calling / Tool Use的原理和实现模式
前端·llm·ai编程