【发布时间】:2020-06-18 02:39:46
【问题描述】:
我正在尝试使用VueX 和来自 rapidapi 的免费 API 来显示数据。不知何故,我无法在组件中正确显示或遍历它。
控制台正确显示对象,但应该显示它的组件却没有。
我做错了什么?
以下是相关代码:
store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
export default new Vuex.Store({
state: {
worldData:
fetch("https://covid-193.p.rapidapi.com/statistics", {
method: "GET",
headers: {
"x-rapidapi-host": "covid-193.p.rapidapi.com",
"x-rapidapi-key": "mySecretKey"
}
})
.then(response => response.json())
.then(data => {
data.response.sort((a, b) => (a.country > b.country ? 1 : -1));
console.log(data.response);
return data.response;
})
},
getters: {
worldData: state => state.worldData,
},
mutations: {
},
actions: {
},
modules: {
}
})
components/mycomponent.vue
<template>
<div >
<div v-for="myData in $store.getters.worldData" :key="myData">{{myData}}</div>
</div>
</template>
【问题讨论】:
-
country是什么数据类型?是字符串还是数字? -
这是一个字符串。如上所述,控制台实际上在检查器中正确显示了对象。 :)