const url=”/mock/products”
const query={pageIndex: 1, pageSize: 5}
- 方法一
const serialize = function(obj) {
var ary = [];
for (var p in obj)
if (obj.hasOwnProperty(p) && obj[p]) {
ary.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
}
return ary.join('&');
};
- 方法二
使用了antd,form表单会有undefined的时候,map方法会把undefined也拼接上去。需要处理query。
const queryStr = Object.keys(query)
.map(key => query[key] && `${encodeURIComponent(key)}=${encodeURIComponent(query[key])}`)
.join('&');
- 方法三
const queryStr = Object.keys(query)
.reduce((ary, key) => {
if (query[key]) {
ary.push(encodeURIComponent(key) + '=' + encodeURIComponent(query[key]));
}
return ary;
}, [])
.join('&');
url += `?${queryStr}`;