ts解析地址栏参数

93 阅读1分钟
function GetRequest() {
const result = {}
const query = window.location.href.split('?')[1]
	if (query) {
	const queryArr = query.split('&')
	queryArr.forEach(item => {
	const value = item.split('=')[1]  
	const key = item.split('=')[0]
	const results=result as any
	results [key] = value
	})
}
    return result  
}


const id= (GetRequest as any)()['id']
const starts= (GetRequest as any)()['startDate'].replace(/\%20/g," ").replace(/[a-zA-Z]/g,' ')

有特殊字符的

getQueryParams (url,name){
// 如果url中有特殊字符则需要进行一下解码
	    url = decodeURI(url)
		if(url.indexOf('?') === -1) return false
		
		const arr1 = url.split('?');
		if (arr1.length >= 1) {
		  const arr2 = arr1[1].split('&');
	     	for (let i = 0; i < arr2.length; i++) {
		      const curArr = arr2[i].split('=');
	  	if(curArr[0] === name){
		return curArr[1]
 }}}}