js动态拼接url参数query

// 定义url字符串拼接的方法
const setUrlQuery = (options) => {
    let {url,query} = options;
    if(!url) return '';
    if(query) {
        let queryArr = [];
        for (const key in query) {
            if (query.hasOwnProperty(key)) {
                queryArr.push(`${key}=${query[key]}`)
            }
        }
        if(url.indexOf('?') !== -1) {
            url =`${url}&${queryArr.join('&')}`
        } else {
            url =`${url}?${queryArr.join('&')}`
        }
    }
    return url;
}

你可能感兴趣的:(js动态拼接url参数query)