mockjs的get传参
前端请求
const { data } = await axios("/video/childcomments", {
params: {
sort: 1,
start: 2,
count: 5,
childCount: 6,
commenIndex: 0,
},
});
const getQuery = urlStr => {
const startIndex = urlStr.indexOf('?');
const strSub = urlStr.substring(startIndex + 1);
const strReplace = strSub.replaceAll('=', '":');
const arr = strReplace.split('&');
arr.forEach((it, i) => arr[i] = '"' + it);
const arrToStr = arr.join(',')
const resStr = `{${arrToStr}}`
const resObj = JSON.parse(resStr); resObj
return resObj
}
Mock.mock(/video\/childcomments.*?/, getInfo => {
const { url } = getInfo
const toObj = getQuery( url)
console.log(toObj);
})
执行结果