javascript - axios 条件参数
问题描述
axios.get(globalConfig.FAKE_API, {
params: {
phone: this.phone,
mail: this.mail
},
})
.then((resp) => {
this.req = resp.data
})
.catch((err) => {
console.log(err)
})
使用 Axios 进行 GET / POST 请求时,有什么方法可以制作条件参数?例如,如果我的mail
参数为空,我不会发送空参数,例如:someurl.com?phone=12312&mail=
解决方案
您可以params
在发出请求之前维护一个变量,并且仅在它具有以下数据时才添加密钥:
const params = {}
if (this.mail) { params.mail = this.mail }
或者你可以像下面这样,我们...()
在括号之间写普通的js代码。我们正在添加一个三元条件。
axios.get(globalConfig.FAKE_API, {
params: {
phone: this.phone,
...(this.mail ? { mail: this.mail } : {})
},
})
推荐阅读
- r - 混淆矩阵 - 错误:`data` 和 `reference` 应该是具有相同级别的因素
- html - CSS全屏背景在移动设备上滚动时在底部显示白条
- javascript - 再一次:SetInterval 不会停止
- r - 合并r中的列值时,如何选择更高(最大值)的值
- r - Separating vector into two columns with dplyr mutate
- javascript - 如何使用moment.js对结果大于24小时的日期和时间求和
- sql - 如何在 SQL 中没有任何相互列或值的情况下将一整列从表复制到另一列?
- json - Flutter:将 JSON 数据发送到 API 获取错误:NoSuchMethodError:Class 'List
' 没有实例方法 'toMap' - python - Python,如何使用 map 函数运行 2 个或多个具有不同值的函数?
- erlang - Erlang主管默默无法启动