获取url的参数

let {location} = window;

const param = (() => {const args = {};let match = null; // 获取问好后面所有的字符串

const search = location.search.substring(1);// 这段正则一般用于提取URL中的参数,会把URL中?后的参数部分以&分割为参数对

const reg = /(?:([^&]+)=([^&]+))/g;

// exec() 方法用于检索字符串中的正则表达式的匹配。返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。

 while ((match = reg.exec(search)) !== null) {

       if (match[2]) {

             // decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。

             args[match[1]] = decodeURIComponent(match[2]);

 } }

 return args;

})();

param.debug = param.debug || window.debug;

export default param;

说明:保存该组件为param.js,ur例子http://222.222.222.222:8080/fed/index.html?cid='2344'&uid='yyu7'

组件调用

import param from 'param';

const data = { cid: param.cid, uid: param.uid};

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,905评论 0 38
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 当给sq...
    xuningbo阅读 10,423评论 2 22
  • 嘘,别回头!那人背后有双眼睛。
    你和海阅读 279评论 0 0
  • 我们太多时候压抑自己,人前总是伪装自己,含蓄、内敛、伪装、安静……然后,夜里,或者无人的地方,一声声叹息。 何...
    higher_lover阅读 145评论 0 0