Js获取url参数
1. 如果参数为字符串:
URL:http://localhost:8088/#/orderDetail?workOrderId=001&type=0&status=processing&event=yes
getQueryVariable(name) {
let reg = new RegExp("(^|&)?" + name + "=([^&]*)(&|$)", "i");
let r = window.location.href.substr(1).match(reg);
if (r != null) {
return decodeURIComponent(r[2]);
};
return null;
},
获取参数workOrderId
的值:
this.getQueryVariable("workOrderId"); // 001
2. 如果参数为对象:
URL: http://localhost:8088/#/orderDetail?data={ "id": "aaa", "pageNo": 1, "pageSize": 10, "totalCount": 1 }
获取参数data中id:
console.log(JSON.parse(this.getQueryVariable("data")).id); // aaa
注意
: 参数为对象时,必须严格遵守JSON语义:{"name": "taohang"}
。