t之前一直都是在小程序上傳遞一個(gè)參數(shù)的,這次做到的項(xiàng)目要做小程序上面?zhèn)鬟f一個(gè)對(duì)象,但是發(fā)現(xiàn)原生的小程序是無(wú)法傳遞一個(gè)對(duì)象參數(shù)的,自己百度了一下,參考鏈接是https://blog.csdn.net/tinson12321/article/details/82981365
參考上面的內(nèi)容 我做了一點(diǎn)改動(dòng),以下是代碼




以下是2個(gè)封裝好的方法代碼
// 構(gòu)建url
export const buildURL = (url, query = {}, isSequence = true) => {
? ? if (!query) return url
? ? const joiner = url.match(/\?/) ? '&' : '?'
? ? const queryStr = Object.keys(query)
? ? ? .map(key => `${key}=${encodeURIComponent(isSequence ? JSON.stringify(query[key]) : query[key])}`)
? ? ? .join('&')
? ? return url + joiner + queryStr
? }
? // 解析query對(duì)象
? export const? decodeQuery = (originQuery = {}, isSequence = true) => {
? ? const result = {}
? ? if (!originQuery) return {}
? ? return Object.keys(originQuery).reduce((prev, curr) => {
? ? ? result[curr] = decodeURIComponent(originQuery[curr])
? ? ? if (isSequence) {
? ? ? ? result[curr] = JSON.parse(result[curr])
? ? ? }
? ? ? return result
? ? }, result)
? }