【发布时间】:2019-01-09 23:57:35
【问题描述】:
对于我的国家/地区列表中的每个国家/地区,我需要使用 axios 进行 api 调用以获得另一个值,这里是 y 组件:
<template>
<div>
<div v-for="(country, i) in countries" :key="i">
<div>{{ county[i.id].count }}</div>
</div>
</div>
</template>
在我的脚本中,我在 mount 上调用我的方法 matchCount 并将值存储在我的县数据对象中:
<script>
export default {
props: {
countries: {
type: Array,
required: true
}
},
data() {
return {
county = {}
};
},
mounted() {
this.matchCount();
},
methods: {
matchCount() {
var paysCount = this.pays;
paysCount.forEach(item => {
this.$axios
.get(
`https://api.com/federation/}/${item.id}/`
)
.then(response => {
this.county[item.id] = {};
this.county[item.id].count = response.data.length.toString();
});
});
}
}
};
</script>
我收到这个错误“TypeError: Cannot read property 'count' of undefined”,我应该如何调用这个方法?
【问题讨论】: