VUE 3显示获取数据v-for
因此,我正在创建一个口袋妖怪应用程序,我想使用API使用API显示口袋妖怪名称:。 我正在对API进行提取请求,然后在模板中显示口袋妖怪名称。当我尝试仅显示1个口袋妖怪时,我有0个问题,但是当我尝试使用V-For显示所有口袋文件时,我会有此错误。 您是否知道为什么我会遇到此错误? net/uo2l8.png“ alt =”在此处输入图像说明”>
<template>
<p class="dark:text-white"> {{pokemons[0].name}} </p> //working
<div v-for="(pokemon, index) in pokemons" :key="'poke'+index"> //not working...
{{ pokemon.name }}
</div>
</template>
<script>
const apiURL = "https://pokeapi.co/api/v2/pokemon/"
export default {
data(){
return{
nextURL:"",
pokemons: [],
};
},
created(){
this.fetchPokemons();
},
methods:{
fetchPokemons(){
fetch(apiURL)
.then( (resp) => {
if(resp.status === 200){
return resp.json();
}
})
.then( (data) => {
console.log(data.results)
// data.results.forEach(pokemon => {
// this.pokemons.push(pokemon)
// });
// this.nextURL = data.next;
this.pokemons = data.results;
console.log(this.pokemons);
})
.catch( (error) => {
console.log(error);
})
}
}
}
</script>
<style>
</style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚将您的代码粘贴到代码笔中,并删除了工作/不工作评论,并且代码运行并显示了名称。
也许问题在于安装此组件的父组件,或者分配:键属性
尝试
:key =“'poke'+index.toString()”
,但是我敢肯定,JS Handels String Integer Convats安静得很好。您使用哪个版本的VUEJS?
从注释中编辑:
带有名称
pokemonlistvue
的父组件将已发布的组件导入为pokemonlistvue
,导致命名冲突。重命名其中的任何一个都解决了这个问题。在发布的错误消息中,在第3行中,它在formatcomponentname 上说
这是一个很好的提示。
I've just pasted your code into a Code Pen and removed the working/not working comments and the code runs and shows the names.
Maybe the problem is in the parent component where this component is mounted, or the assignment of the :key attribute
try
:key="'poke'+index.toString()"
, but I'm pretty sure js handels string integer concats quiet well.Which version of vuejs do you use?
Edit from comments:
The parent component with the name
PokemonListVue
imported the posted component asPokemonListVue
which resulted in a naming conflict. Renaming either one of those solves the issue.In the error message posted, in line 3 it says
at formatComponentName
this is a good hint.