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}`;