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

👉 立即开发

相关推荐
whinc17 小时前
Node.js技术周刊 2026年第14周
javascript·node.js
这个昵称也不能用吗?17 小时前
eas 热更新相关
前端
KaMeidebaby18 小时前
卡梅德生物技术快报|葫芦科植物遗传转化:Fast‑TrACC 工程化优化:葫芦科植物遗传转化效率提升与成本控制
前端·其他·百度·新浪微博
换日线°18 小时前
vue 加入购物车抛物线动画
前端·javascript·vue.js
计算机学姐18 小时前
基于微信小程序的图书馆座位预约系统【uniapp+springboot+vue】
vue.js·spring boot·微信小程序·小程序·java-ee·uni-app·intellij-idea
切糕师学AI19 小时前
为什么你的 SPA 网址必须包含 `#`?—— 前端路由 Hash 模式深度解析
前端·spa 网址·hash路由
冴羽19 小时前
超越Vibe Coding —— AI 辅助编程进阶指南
前端·javascript·ai编程
流氓也是种气质 _Cookie19 小时前
Chrome Performance常见名词解释(FP, FCP, LCP, DCL, FMP, TTI, TBT, FID, CLS)
开发语言·javascript·ecmascript
MXN_小南学前端19 小时前
自制和整理常用前端 AI Skills分享,从需求到页面(附github地址)
前端·ai编程
yuki_uix19 小时前
双 RAF + MutationObserver:微前端跳转后的滚动复原完整方案
前端