1、导入依赖"@tencent/wechat_open_sdk": “1.0.3”,一定是要1.0.3版本以上,才支持
2、直接拿代码
import * as wxopensdk from '@tencent/wechat_open_sdk'; // 导入微信 SDK
import { LaunchMiniProgramModel } from '../../Models/LaunchMiniProgramModel';
export const WX_APP_ID = "这个是在微信开放平台申请到的appid"
export const WXApi = wxopensdk.WXAPIFactory.createWXAPI(WX_APP_ID)
//调用该方法可直接跳转
async function jumpLaunchMiniProgram(context: common.UIAbilityContext): Promise<void> {
//获取小程序信息 以下是一个示例
let getLaunchMiniProgramModel = new LaunchMiniProgramModel("小程序的原始id","小程序页面的可带参路径", 0)
//跳转微信小程序
let launchMiniProgramReq = new wxopensdk.LaunchMiniProgramReq;
launchMiniProgramReq.userName = getLaunchMiniProgramModel.userName; //拉起的小程序的原始id
launchMiniProgramReq.path = getLaunchMiniProgramModel.applitePath; //拉起小程序页面的可带参路径,不填默认拉起小程序首页,对于小游戏,可以只传入 query 部分,来实现传参效果,如:传入 "?foo=bar"。
launchMiniProgramReq.miniprogramType = getLaunchMiniProgramModel.type; //拉起小程序的类型 0-正式版 1-开发版 2-体验版
let success = await WXApi.sendReq(context, launchMiniProgramReq);
console.info("是否跳转成功:"+success)
}
export class LaunchMiniProgramModel {
userName: string;
applitePath: string;
type: number
constructor(userName: string, applitePath: string, type: number) {
this.userName = userName;
this.applitePath = applitePath;
this.type = type;
}
}
3、如果还没有申请到小程序的appid,按照以下文档去申请
https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Launching_a_Mini_Program/OHOS_Development_example.html