获取路由参数 getUrlParam

1.封装

var self = {
  getUrlParam(search = "", key) {
    var param = [];
    var result = "";
    if (search.length > 0) {
      if (search.indexOf("&&") == -1) {
        param = search.split("?")[1].split("&");
      } else {
        param = search.split("?")[1].split("&&");
      }
      for (var i = 0; i < param.length; i++) {
        if (param[i].indexOf(key + "=") != -1) {
          result = param[i].split("=")[1];
          break;
        }
      }
    }
    return result;
  },
};

module.exports = self;

2.调用

import func from "UTIL/commonFunc";

let id = func.getUrlParam(location.search, "id");

.

posted @ 2020-07-17 12:00  每天都要进步一点点  阅读(1505)  评论(0编辑  收藏  举报