[英]How can I pass query parameters optional using axios in vue?
I try like this:我尝试这样:
const actions = {
getData ({ commit }, payload) {
const params = {}
if(payload) {
if (payload.dateFrom) { params.date_from = payload.dateFrom }
if (payload.dateTo) { params.date_to = payload.dateTo}
}
axios
.get(`${api}/reports/list`, {params})
.then(r => r.data)
.then(res => {
console.log(res)
})
}
}
If I console.log(payload), the result like this:如果我 console.log(payload),结果如下:
{from: '2022-07-01', to: '2022-07-30'} {从:'2022-07-01',到:'2022-07-30'}
payload is optional有效载荷是可选的
My code is working.我的代码正在运行。 But I want to know if there is a more concise way to express it .
但我想知道是否有更简洁的表达方式。
You can conditional add property to object like this:您可以像这样有条件地将属性添加到 object :
const actions = { getData({ commit }, payload) { const params = {...(payload?.dateFrom && { date_from: payload.dateFrom }), ...(payload?.dateTo && { date_to: payload.dateTo }) }; axios.get(`${api}/reports/list`, { params }).then((r) => r.data).then((res) => { console.log(res); }); } };
One idea is to default the param, then conditionally add its props, like...一个想法是默认参数,然后有条件地添加它的道具,比如......
getData ({ commit }, payload={}) {
const params = {
...(payload.from ? {from: payload.from}: {}),
...(payload.to ? {to: payload.to}: {}),
}
axios
.get(`${api}/reports/list`, {params}) // etc
That's still a little bit clumsy conditional inclusion of key-value pairs.键值对的条件包含仍然有点笨拙。 Another idea, since it's really a kind of pick, you could code that explicitly (and reuse it elsewhere)...
另一个想法,因为它真的是一种选择,你可以明确地编码(并在其他地方重用它)......
// pick, thanks to https://stackoverflow.com/a/56592365/294949
function pick(obj={}, keys=[])
return Object.fromEntries(
keys.filter(key => key in obj).map(key => [key, obj[key]])
);
}
Then getData
is...然后
getData
是...
getData ({ commit }, payload) {
const params = pick(payload, ['from', 'to']);
axios
.get(`${api}/reports/list`, {params}) // etc
edit if the key names will change, then it's not a pick.编辑如果键名会改变,那么它不是一个选择。 The first idea still works, and given the switch is probably close to minimal code...
第一个想法仍然有效,并且考虑到开关可能接近最少的代码......
getData ({ commit }, payload={}) {
const params = {
...(payload.dateFrom ? {date_from: payload.dateFrom}: {}),
...(payload.dateTo ? {date_to: payload.dateTo}: {}),
}
axios
.get(`${api}/reports/list`, {params}) // etc
try this.尝试这个。
getTest({ commit }, { from = undefined, to = undefined }) { axios.get(`api/reports/list`, { params: { from, to } }).then((r) => r.data).then((res) => { console.log(res); }); }
so when you want to use the action you can do these所以当你想使用动作时,你可以做这些
this.$store.dispatch("getTest", { from: 121 }); this.$store.dispatch("getTest", { from: 121 });
this.$store.dispatch("getTest",{}); this.$store.dispatch("getTest",{}); //at least an empty Object
//至少一个空的 Object
Maybe you can try this:也许你可以试试这个:
const actions = {
getData ({ commit }, payload) {
const params = { ...payload }
axios
.get(`${api}/reports/list`, { params })
.then(r => r.data)
.then(res => {
console.log(res)
})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.