截取url参数

onLoad(){
    let params = this.getRequestParams();
    this.code= params["code"];
    this.state= params["state"];
},
methods:{
    getRequestParams() {
       let url = location.href;
       // let url = 'http://localhost:8080/#/pages/user/login?code=113&state=100';
       let requestParams = {};
       if (url.indexOf("?") !== -1) {
           let str = url.substr(url.indexOf("?") + 1); //截取?后面的内容作为字符串
           let strs = str.split("&"); //将字符串内容以&分隔为一个数组
           for (let i = 0; i < strs.length; i++) {
               requestParams[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
               // 将数组元素中'='左边的内容作为对象的属性名,'='右边的内容作为对象对应属性的属性值
            }
       }
       console.log(requestParams);
       return requestParams;
     },
}

console.log

{
	"code": "113",
	"state": "100"
}

上一篇:【剑指offer第16天】排序


下一篇:NC55最长公共前缀