4***@qq.com
4***@qq.com
  • 发布:2019-01-01 19:38
  • 更新:2019-11-14 17:09
  • 阅读:12518

uni-app 打包 H5 单页面应用 渠道参数的传递和获取 如: index.html?channelid=icefrog

分类:uni-app

业务前景:
采用uni-app开发的项目 发型打包生成 H5-手机版 单页面应用 打包出来的总入口页面是index.html

业务需求:
在外界直接打开index.html 总入口页面时需要传递参数及获取参数 用于区分渠道来源或分享标记

相关尝试:
this.$route.query this.$route.params 等操作无效 获取不到首页面的参数

解决方案:
在App.vue 应用生命周期的 onLaunch()中采用JS直接提取链接参数 然后针对参数进行拆分
如:https://www.xxx.com/h5/index.html?sceneid=h5&wxcode=xxx&puid=fff

onLaunch(){

let tmpUrlSearch = window.location.search;// 得到:?sceneid=h5&wxcode=xxx&puid=fff
let tmpParas=GetRequestParameters(tmpUrlSearch);
let tmpSceneID = tmpParas["sceneid"];//提取参数
};

网上抄的一段儿方法:
export const GetRequestParameters=(locationsearch) =>{

let url = locationsearch;  
let theRequest = new Object();  
if (url.indexOf("?") != -1) {  
    let str = url.substr(1);  
    let strs = str.split("&");  
    for (let i = 0; i < strs.length; i++) {  
        theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);  
    }  
}  
return theRequest;  

}

1 关注 分享
982585390

要回复文章请先登录注册

非专业开发人员

非专业开发人员

回复 3***@qq.com :
只能打包才能获取,
要想不打包获取,就改一下上面代码

let url = window.location.href;
console.log('===----'+url)
var urlArray = url.split('?');
var tempStr = '?'+urlArray[1];
let tmpParas = GetRequestParameters(tempStr);
var tmpSceneID = tmpParas["userName"];//提取参数
tmpSceneID = tmpSceneID.replace('#/', '');
2019-11-14 17:09
非专业开发人员

非专业开发人员

这是打包后才能拿到吗? 直接在浏览器上运行,找不到window.location.search,search这个方法
2019-11-14 16:49
3***@qq.com

3***@qq.com

这是打包后才能拿到吗
2019-04-02 10:49