javascript - 使用 VueX Store 中的计算属性进行 Ajax 调用的正确方法是什么
问题描述
如何使用调用参数之一作为 VueX 中的计算状态进行 ajax 调用。例如,如果我创建 this.$axios.get('someUrl/' + accID ),accID 是来自 VueX 的计算属性(通过 MapState)。有时 id 返回“未定义”,我怀疑这是由于 axios 在从商店填充 id 数据之前发出 get 请求
我曾尝试在 Vue 组件中的“accID”上使用 watch() 来等待 accID 解决但无济于事
//部分代码
computed: {
...mapState(['user']),
},
async fetchData() {
const [foodData, option] = await Promise.all([
this.$axios({
url: `food/${this.user.accID}`,
method: 'get',
}),
this.$axios({
url: 'options',
method: 'get',
})
])
//foodData returns undefined because user.accID is undefined (sometimes)
期待
this.$axios({ url:'food/12345', method: 'get' })
反而
this.$axios({ url:'food/undefined', method: 'get' })
解决方案
更改${user.accID}
为:${this.user.accID}
_url
url: `food/${this.user.accID}`,
推荐阅读
- javascript - 在数据表上显示列的总和(总计)并导出到 excel
- bash - 如何在字符串中的特定模式之后提取特定文本
- highcharts - Highstock - 软最小值和最大值不工作
- android - Android apk Build 输出文件夹中充斥着大量带有时间戳的 apk
- html - NativeScript WebView 未加载 html 文件
- android - 如何从 TextInputLayout 中删除白框
- python-3.x - 如何在 Spacy 中优先考虑基于规则的匹配而不是经过训练的 NER 模型?
- testing - 分发 Flutter 应用程序 Android 和 iOS 的最佳方式。Flutter Beta 分发的最佳实践
- php - Laravel:将 index.php 放在公共文件夹的子文件夹中
- python - 如何在for循环中将代码更改为同步模式