首先获取到url地址
let geturl = window.location.href
// http://localhost:8081/#/pages/index/index?qycode=1001&qyname=%E4%BC%81%E4%B8%9A%E5%BF%99
方式一
let getqyinfo = geturl.split('?')[1] //qycode=1001&qyname=%E4%BC%81%E4%B8%9A%E5%BF%99 截取到参数部分
let getqys = new URLSearchParams('?'+getqyinfo) //将参数放在URLSearchParams函数中
let getqycode = getqys.get('qycode') //1001
let getqyname = getqys.get('qyname') //%E4%BC%81%E4%B8%9A%E5%BF%99
方式二
let getqyinfo = geturl.split('?')[1] //qycode=1001&qyname=%E4%BC%81%E4%B8%9A%E5%BF%99
let getqys = getqyinfo.split('&') //['qycode=1001', 'qyname=%E4%BC%81%E4%B8%9A%E5%BF%99%E5%95%8A%E5%95%8A%E5%95%8A']
let getqycode = getqys[0].split('=')[1]
let getqyname = getqys[1].split('=')[1]
console.log('qycode:',getqycode,'getqyname:',getqyname)
方式三
let getqyinfo = geturl.split('?')[1]
let getqys = getqyinfo.split('&')
let obj = {} //创建空对象,接收截取的参数
for(let i = 0;i < getqys.length;i++ ){
console.log(i)
let item = getqys[i].split('=')
let key = item[0]
let value = item[1]
obj[key] = value
}
console.log(obj)
作者:资深Bug开发大师
链接:https://www.jianshu.com/p/72289467a54b
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
网友评论