首先获取到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
- 3
- 4
方式二
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)
- 3
- 4
- 5
方式三
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)
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11