1. 程式人生 > >解析url並獲取其引數

解析url並獲取其引數

/**
 * 解析url引數
* @example ?id=123&a=b 在Vue路由中會出現
id=123&a=b#/CoIndex
* @return Object {id:123, a:b} */let url = window.location.searchexport function urlParse () { if (!url) { return} let obj = {} let reg = /[?&][^?&]+=[^?&#]+/glet arr = url.match(reg) // ['?id=123', '&a=b']if
(arr) { arr.forEach((item) => { let tempArr = item.substring(1).split('=') let key = decodeURIComponent(tempArr[0]) let val = decodeURIComponent(tempArr[1]) obj[key] = val}) return obj}}