干维修越忙越穷?修一万台设备,不如啃透一块板子
2026/7/23 21:02:44
wx.navigateToMiniProgram是微信小程序提供的 API,用于从当前小程序跳转到另一个小程序。这个功能在实现小程序之间的业务联动、数据传递等场景中非常实用。
wx.navigateToMiniProgram({appId:'目标小程序的appId',path:'目标小程序的页面路径',extraData:{// 传递给目标小程序的数据},envVersion:'release',// 环境版本success:function(res){// 跳转成功的回调},fail:function(err){// 跳转失败的回调},complete:function(){// 跳转完成的回调}})| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| appId | String | 是 | 要跳转的小程序 appId |
| path | String | 否 | 打开的页面路径,如果为空则打开首页 |
| extraData | Object | 否 | 需要传递给目标小程序的数据,目标小程序可在App.onLaunch、App.onShow中获取到这份数据 |
| envVersion | String | 否 | 要打开的小程序版本。有效值:develop(开发版)、trial(体验版)、release(正式版)。默认值为 release |
| success | Function | 否 | 接口调用成功的回调函数 |
| fail | Function | 否 | 接口调用失败的回调函数 |
| complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
// 跳转到目标小程序handleJumpToMiniProgram(){wx.navigateToMiniProgram({appId:'wx1234567890abcdef',path:'pages/index/index?id=123',extraData:{userId:'user123',orderId:'order456',timestamp:Date.now()},envVersion:'release',success:(res)=>{console.log('跳转成功',res);wx.showToast({title:'跳转成功',icon:'success'});},fail:(err)=>{console.error('跳转失败',err);wx.showToast({title:'跳转失败',icon:'none'});}});}// 目标小程序的 app.jsApp({onLaunch(options){// 从其他小程序跳转过来时,可以获取传递的数据if(options.referrerInfo&&options.referrerInfo.appId){console.log('来源小程序 appId:',options.referrerInfo.appId);console.log('传递的数据:',options.referrerInfo.extraData);// 处理传递过来的数据const{userId,orderId}=options.referrerInfo.extraData||{};if(userId){// 根据 userId 进行相关业务处理this.handleUserData(userId);}}},onShow(options){// 从后台进入前台时也会触发if(options.referrerInfo&&options.referrerInfo.appId){console.log('从其他小程序返回',options.referrerInfo);}},handleUserData(userId){// 处理用户数据}})// 根据环境动态选择 appIdgetTargetAppId(){// 开发环境if(process.env.NODE_ENV==='development'){return'wx_dev_appid';}// 生产环境return'wx_prod_appid';},// 跳转前检查是否已授权asynchandleJumpWithCheck(){// 检查用户是否已登录constuserInfo=wx.getStorageSync('userInfo');if(!userInfo){wx.showModal({title:'提示',content:'请先登录',success:(res)=>{if(res.confirm){// 跳转到登录页wx.navigateTo({url:'/pages/login/login'});}}});return;}// 检查目标小程序是否在关联列表中consttargetAppId=this.getTargetAppId();constisLinked=awaitthis.checkMiniProgramLink(targetAppId);if(!isLinked){wx.showToast({title:'目标小程序未关联',icon:'none'});return;}// 执行跳转wx.navigateToMiniProgram({appId:targetAppId,path:'pages/detail/detail',extraData:{userId:userInfo.id,token:userInfo.token},success:()=>{console.log('跳转成功');},fail:(err)=>{console.error('跳转失败',err);wx.showToast({title:err.errMsg||'跳转失败',icon:'none'});}});}// utils/miniProgram.js/** * 跳转到其他小程序 * @param {Object} options 跳转配置 * @returns {Promise} */exportfunctionnavigateToMiniProgram(options){returnnewPromise((resolve,reject)=>{wx.navigateToMiniProgram({appId:options.appId,path:options.path||'',extraData:options.extraData||{},envVersion:options.envVersion||'release',success:(res)=>{resolve(res);},fail:(err)=>{reject(err);}});});}// 使用示例import{navigateToMiniProgram}from'@/utils/miniProgram';asynchandleJump(){try{awaitnavigateToMiniProgram({appId:'wx1234567890abcdef',path:'pages/index/index',extraData:{userId:'123'}});console.log('跳转成功');}catch(error){console.error('跳转失败',error);wx.showToast({title:'跳转失败',icon:'none'});}}关联小程序:需要在微信公众平台配置关联的小程序,否则无法跳转
用户授权:首次跳转需要用户确认授权
域名白名单:确保目标小程序已配置合法域名
envVersion参数需要根据实际情况设置extraData对象大小不能超过 128KB// 检查基础库版本if(wx.canIUse('navigateToMiniProgram')){// 支持跳转wx.navigateToMiniProgram({...});}else{// 不支持,提示用户升级微信版本wx.showModal({title:'提示',content:'当前微信版本过低,无法使用此功能,请升级到最新微信版本后重试。',showCancel:false});}问题:传递的数据在目标小程序中获取不到
解决方案:
extraData传递App.onLaunch或App.onShow中正确获取// 正确获取方式App({onLaunch(options){// 从其他小程序跳转过来if(options.referrerInfo&&options.referrerInfo.appId){constextraData=options.referrerInfo.extraData;console.log('接收到的数据:',extraData);}}})目标小程序可以通过wx.navigateBackMiniProgram返回到原小程序:
// 在目标小程序中返回wx.navigateBackMiniProgram({extraData:{result:'处理完成'},success:()=>{console.log('返回成功');}});在原小程序中接收返回数据:
App({onShow(options){// 从其他小程序返回if(options.referrerInfo&&options.referrerInfo.appId){constextraData=options.referrerInfo.extraData;console.log('返回的数据:',extraData);}}})// config/miniPrograms.jsexportconstMINI_PROGRAMS={PAYMENT:{appId:'wx_payment_appid',name:'支付小程序',paths:{PAY:'pages/pay/pay',ORDER:'pages/order/order'}},TOOLS:{appId:'wx_tools_appid',name:'工具小程序',paths:{INDEX:'pages/index/index'}}};// 使用import{MINI_PROGRAMS}from'@/config/miniPrograms';wx.navigateToMiniProgram({appId:MINI_PROGRAMS.PAYMENT.appId,path:MINI_PROGRAMS.PAYMENT.paths.PAY});// 统一的错误处理functionhandleJumpError(err){consterrorMap={'fail cancel':'用户取消跳转','fail app not found':'未找到目标小程序','fail app not support navigate':'目标小程序不支持跳转','fail invalid appid':'appId 无效'};consterrorMsg=errorMap[err.errMsg]||err.errMsg||'跳转失败';wx.showToast({title:errorMsg,icon:'none',duration:2000});// 上报错误日志console.error('跳转失败:',err);}// 跳转前验证asyncfunctionvalidateBeforeJump(appId){// 1. 检查网络constnetworkType=awaitgetNetworkType();if(networkType==='none'){wx.showToast({title:'网络不可用',icon:'none'});returnfalse;}// 2. 检查是否关联constisLinked=awaitcheckMiniProgramLink(appId);if(!isLinked){wx.showToast({title:'小程序未关联',icon:'none'});returnfalse;}// 3. 检查基础库版本if(!wx.canIUse('navigateToMiniProgram')){wx.showModal({title:'提示',content:'当前微信版本过低,请升级后重试',showCancel:false});returnfalse;}returntrue;}wx.navigateToMiniProgram是小程序生态中实现跨小程序跳转的重要 API,合理使用可以实现:
在使用过程中需要注意:
希望这篇技术分享能帮助大家更好地使用navigateToMiniProgramAPI!
参考资料: