小程序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

👉 立即开发

相关推荐
沙盘客6 分钟前
AFSIM 示例解读(03)· 六自由度飞行仿真 six_dof(下):机场运作与弹道导弹投送
java·javascript·经验分享
Codiggerworld18 分钟前
Chrome DevTools 隐藏神技:这 5 个调试技巧你可能从来没用过
前端·chrome·chrome devtools
旋生万物21 分钟前
【终极实战】用Python从零“生成“一个宇宙:螺旋干涉模型的代码实现
开发语言·前端·人工智能·react.js·php·wpf
IT_陈寒44 分钟前
被Java的final坑惨了,这些细节你可能也忽略了
前端·人工智能·后端
kyriewen1 小时前
我带着DeepSeek Harness跑了一周真实需求——这份避坑速查表请收好
前端·ai编程·deepseek
计算机魔术师1 小时前
OpenAI不单独发o3了,Altman说GPT-5才是终局
前端
一座古城2 小时前
Claude Code 架构源码解析:AI 应用开发的范式跃迁
前端·后端
K哥爬虫2 小时前
【JS 逆向百例】Vaptcha V4 手势验证码逆向分析
前端·后端
用户921080262862 小时前
复盘 AI Coding 工作台的流式链路:从 Sender 输入到 SSE 分流,再到 BubbleList 和右侧预览
前端
了不起的小明2 小时前
SwiftMesh:本地 3D 模型查看器,加密交付 + 转盘录屏一站式搞定,开源免费
前端