识别小程序码得路径和参数?
发布于 6 年前 作者 swei 5935 次浏览 来自 官方Issues

例如:有多个小程序码  码1:path = 123  码2: path=456

怎么样才能快速识别他们得参数和路径呢?

2 回复

getQueryString 可封装到 util下。下次直接调用 util.getQueryString[‘参数名’]

//页面初始化加载

  onLoad(options) {

    if (!options.scene) { //判断不是从小程序码进来的
      this.setData({
        partJobId: options.partJobId ||  '',
      })
    } else {
      var getQueryString = {}
      var strs = decodeURIComponent(options.scene).split('&') //以&分割
      //取得全部并赋值
      for (var i = 0; i < strs.length; i++) {
        getQueryString[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
      }
      this.setData({
        partJobId: getQueryString['partJobId'] || ''
      })
    }
  },

function GetQueryString(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null){
return r[2];
}
return null;
}
回到顶部