navigateToMiniProgram变更的低版本兼容
wx.navigateToMiniProgram接口即将废弃,转为使用navigator组件,但是版本最低要求2.0.7,@官方,请问对于低版本的我怎么做兼容呢?
2 回复
<template> <div> <navigator v- if = "canIUseNavigator" :class= "className" :target= "target" :app-id= "appId" :open-type= "openType" :path= "path" :extra-data= "extraData" :version= "version" > <slot></slot> </navigator> <div v- else :class= "className" [@click](/user/click)= "gotoMiniProgram" > <slot></slot> </div> </div> </template> <script> /** * 注意mpvue暂时不支持slot(scoped) */ const app = getApp(); export default { /** * 页面所需数据 * [@return](/user/return) {Object} 页面数据 */ data: function () { return { canIUseNavigator: false // 是否启用navigator组件,根据基础版本号判断是否大于2.0.7 }; }, props: { className: String, target: String, openType: String, appId: String, path: String, extraData: Object, version: String }, /** * 页面初始化,获取版本信息 * [@return](/user/return) {Void} 无 */ mounted: function () { wx.getSystemInfo({ success: res => { console.log(res.SDKVersion); let baseVersion = res.SDKVersion.split( "." ).join( "" ) - 0; if (baseVersion >= 207) { console.log( "使用navigator组件打开小程序" ); this .canIUseNavigator = true ; } else { console.log( "使用wx.navigateToMiniProgram方法打开小程序" ); this .canIUseNavigator = false ; } } }); }, methods: { /** * 小程序跳转,基础库低于2.0.7版本使用 * [@return](/user/return) {Void} 无 */ gotoMiniProgram() { wx.navigateToMiniProgram({ appId: this .appId, path: this .path, extraData: this .extraData || null , envVersion: this .version, success: () => { console.log(`appId为:${ this .appId}的小程序被打开`); }, fail: () => { console.log(`appId为:${ this .appId}的小程序打开失败`); } }); } } }; </script> |
基于mpvue开发,提供一下思路。
暂时不可以用wx.canIUse进行判断。