javascript - 尝试从 vue.js 中的 http 请求推送到数组时出错
问题描述
我正在尝试将对象推送到来自 axios 获取请求的响应中,但我总是收到“推送不是函数”错误
我试图推入http请求的.then块
ps:我正在关注来自 vuejs 网站的示例
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!',
bitCoinValue: null
},
mounted() {
this.getBitcoinValue();
},
filters: {
currencydecimal(value) {
return value.toFixed(2);
}
},
methods: {
getBitcoinValue: function () {
axios.get('https://api.coindesk.com/v1/bpi/currentprice.json')
.then(response => {
this.bitCoinValue = response.data.bpi || [];
this.bitCoinValue.push({code: 'BRL', description: 'Reais', symbol: 'R$', rate_float: 25.50});
});
}
}
})
这是错误消息:
Uncaught (in promise) TypeError: this.bitCoinValue.push is not a function at site.js:21
解决方案
在浏览器中打开您的 API 端点,我发现"bpi"
响应 JSON 中的键不是数组而是对象。因此,.push()
您需要直接设置键,而不是 ing 值,即this.bitCoinValue.BRL = {...};
.
推荐阅读
- android - 空对象引用上的“java.lang.Class java.lang.Object.getClass()”
- vba - 使用 vba 创建/复制(所有帐户上的此规则)outlook
- reactjs - 将道具从类传递到嵌套的功能组件
- java - MySQLSyntaxErrorException 用于在 MySQL 工作台上正常工作的连接命令
- python - 我有我的伪代码,但我不能在 python 中编写循环之一
- sql - 在laravel中选择表和关系的特定列
- php - 如果多个记录在单个 ID 上,如何获取单个记录?
- vaadin - Vaadin mytheme 样式表在本地主机上不起作用
- r - 如何在R中将数据帧转换为每小时时间序列
- sql-server - 如何不导入双行