@zstings/utils
Version:
javascript、typescript 工具函数库 文档地址 [utils 文档](https://zstings.github.io/utils/)
38 lines (37 loc) • 1.44 kB
TypeScript
/**
* 获取url上的参数
* @param option 可选的对象
* @param option.url url地址,默认window.location.href, 非必填
* @param option.type 类型,默认all, 非必填, 可选值:all, query, hash
* @throws 参数错误, 应该传入一个对象 option不是对象时触发
* @throws url参数错误,不是有效的 url不是有效链接时触发
* @throws type 参数错误, 应该传入一个字符串 'search' | 'hash' | 'all'
* @return 由参数组成的对象
* @category URL
* @example
* 支持search和hash中取值,如果search和hash中有相同的参数,则默认使用search。
* 不传值时,默认从window.location.href中取值
* ```ts
* getUrlQuery() => {a:'1',b:'2'}
* // => window.location.href: https://a.b.com/?a=1&b=2
* ```
* @example
* 从参数的url上取值
* ```ts
* getUrlQuery({url: 'http://a.b.com/?a=1&b=2#/index/?c=3&b=4'}) // => {a: '1', b: '2', c: '3'}
* ```
* @example
* 从参数的url上取值,只在search中取值
* ```ts
* getUrlQuery({url: 'http://a.b.com/?a=1&b=2#/index/?c=3&b=4', type: 'search'}) // => {id: '1', b: '2'}
* ```
* @example
* 从参数的url上取值,只在hash中取值
* ```ts
* getUrlQuery({url: 'http://a.b.com/?a=1&b=2#/index/?c=3&b=4', type: 'hash'}) // => {c: '3', b: '4'}
* ```
*/
export default function getUrlQuery(option?: {
url?: string;
type?: 'search' | 'hash' | 'all';
}): Record<string, any>;