各位大佬,请教一下,鸿蒙api里 拷贝过来的代码 let uiContext = getContext(this); 这段代码里 this 报错 咋整,好多鸿蒙api都会用到这个getContext(this),不知道咋绕过 编译器会报错 Using "this" inside stand-alone functions is not supported (arkts-no-standalone-this)
8***@qq.com (作者)
不传this确实不报错了,但是预览失败了。我想封装uts预览文件插件,用的是https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/preview-filepreview-V5
2024-08-26 15:00
DCloud_UNI_LXH
回复 8***@qq.com: 试试
import { getAbilityContext } from '@dcloudio/uni-runtime';
使用getAbilityContext()!
替代getContext(this)
2024-08-26 19:36
妈惹法克儿
回复 DCloud_UNI_LXH: 在 uts 里修改了之后还是一样,实现小程序跳转的效果
export async function LaunchMiniProgram(userName: string, path: string, miniprogramType = 1) {
try {
let ctx = getAbilityContext();
let req = new wxopensdk.LaunchMiniProgramReq;
req.userName = userName;
req.path = path;
req.miniprogramType = miniprogramType;
let success = await WXApi.sendReq(ctx, req)
return Promise.resolve(success)
} catch (e) {
}
2025-03-31 14:34
DCloud_UNI_LXH
回复 妈惹法克儿: 这个是需要什么 context?UIContext?
2025-03-31 16:44
妈惹法克儿
回复 DCloud_UNI_LXH:
微信官方示例是 uicontext,下面是微信官方的代码,这个是微信的文档:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Launching_a_Mini_Program/OHOS_Development_example.html
import * as wxopensdk from '@tencent/wechat_open_sdk'; // 导入微信 SDK
let context = getContext(this) as common.UIAbilityContext; // 假定我们在组件环境内调用
let launchMiniProgramReq = new wxopensdk.LaunchMiniProgramReq;
launchMiniProgramReq.userName = userName; //拉起的小程序的原始id
launchMiniProgramReq.path = path; //拉起小程序页面的可带参路径,不填默认拉起小程序首页,对于小游戏,可以只传入 query 部分,来实现传参效果,如:传入 "?foo=bar"。
launchMiniProgramReq.miniProgramType = miniProgramType; //拉起小程序的类型 0-正式版 1-开发版 2-体验版
let success = await WXApi.sendReq(context, launchMiniProgramReq);
2025-03-31 17:03
DCloud_UNI_LXH
回复 妈惹法克儿: 不会阻碍使用 getContext 就是不能传 this。使用 getAbilityContext 会报什么错误?如果使用 getContext() 呢?
2025-03-31 17:07
妈惹法克儿
回复 DCloud_UNI_LXH: getContext() 可以了,排查了问题,是在 uts 里使用 async await 导出后使用,移除后能运行,uts 里不能使用是吧?
2025-04-07 11:05