1***@qq.com
1***@qq.com
  • 发布:2026-10-20 14:54
  • 更新:2026-10-20 15:18
  • 阅读:51

鸿蒙Next调用小程序实例关闭 this.mp.close()方法抛异常

分类:鸿蒙Next

因为出现already opened错误闪退,是因为打开相同实例;
所以打开前需要关掉旧实例,然后调用小程序实例关闭 this.mp.close()方法抛异常;

/**

  • 原生通过 openUniMP 启动小程序
  • @param path 小程序界面路径
  • @param record 传递给小程序的额外数据,小程序中onLaunch,uni.getLaunchOptionsSync可以获取
  • @param isPrefix 是否追加前缀 (默认前缀:pages/index/index?pathKey=)
  • @param callback 回调
    */
    private openUniMP(path: string, record: Record<string, string | object | boolean | number> | undefined,
    isPrefix: boolean, callback: IOnOtherUniMPEventCallBack) {
    let iOpenUniMPConfig: IOpenUniMPConfig = {
    showCapsuleButton: false,
    showSplashScreen: false,
    animationType: UniAnimationTypeEnum.FADE_IN,
    // animationDuration: 50,动画持续时间(默认值:300)不宜过长,建议 200-500ms
    redirectPath: isPrefix ? (this.rootPath path) : path,
    extraData: this.getExtraData(record),
    };
    if (!this.mp) {
    HdlHiLogLogic.getInstance().debug('小程序还没有初始化---无法关闭uni', true);
    return;
    }
    try {
    this.mp.close();//抛异常
    } catch (e) {
    HdlHiLogLogic.getInstance().error(关闭旧UniMP实例失败: ${e});
    }
    this.mp = null;
    // 启动小程序
    this.mp = openUniMP(this.uniAppId, iOpenUniMPConfig)
    //原生收到收到小程序消息
    this.mp.on('uniMPEvent', (event, data: ESObject, notify) => {
    HdlHiLogLogic.getInstance().debug(收到Uni事件:${event}\r\n${JSON.stringify(data)});
    if (callback != null) {
    callback.onOtherUniMPEventReceive(this.uniAppId, event, data, notify);
    }
    })

    }

2026-10-20 14:54 负责人:无 分享
已邀请:
Ask小助手

Ask小助手

欢迎到专业群( uni-app鸿蒙化技术交流群 ) 咨询,群中有相关专业的管理员和群友。

您好,针对您在鸿蒙 Next 平台调用 this.mp.close() 方法抛异常导致 App 崩溃的问题,这是一个在旧版本 uni-app-runtime 依赖中存在的已知问题。

根据社区反馈和官方回复,建议您将 uni-app-runtime 依赖更新到 2.3.7 或更高版本,该版本已修复此崩溃问题。详情可以参考以下社区问答:

此外,根据鸿蒙平台官方文档,宿主 App 关闭小程序的方法是调用 uniMP.close(),同时可以通过 uniMP.on('close', () => {...}) 来监听小程序的关闭事件。详情请参考:

内容为 AI 生成,仅供参考
1***@qq.com

1***@qq.com (作者) - 疯狂打码中。。。

我使用是 "@dcloudio/uni-app-runtime": "^5.26.2026091401",
抛异常:Waiting window

要回复问题请先登录或注册