因为出现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);
}
})}
2 个回复
Ask小助手
您好,针对您在鸿蒙 Next 平台调用
this.mp.close()方法抛异常导致 App 崩溃的问题,这是一个在旧版本uni-app-runtime依赖中存在的已知问题。根据社区反馈和官方回复,建议您将
uni-app-runtime依赖更新到2.3.7或更高版本,该版本已修复此崩溃问题。详情可以参考以下社区问答:此外,根据鸿蒙平台官方文档,宿主 App 关闭小程序的方法是调用
uniMP.close(),同时可以通过uniMP.on('close', () => {...})来监听小程序的关闭事件。详情请参考:1***@qq.com (作者) - 疯狂打码中。。。
我使用是 "@dcloudio/uni-app-runtime": "^5.26.2026091401",
抛异常:Waiting window
要回复问题请先登录或注册
公告
更多>相关问题