VUE 3显示获取数据v-for

发布于 2025-01-29 12:28:43 字数 1611 浏览 1 评论 0 原文

因此,我正在创建一个口袋妖怪应用程序,我想使用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>

So, I'm creating a Pokemon application and I would like to display the pokemon names using the api : https://pokeapi.co/api/v2/pokemon/.
I'm doing a fetch request on the api and then display the pokemon names in my template. I have 0 problem when I try to display only 1 pokemon but I have this error when I try to display all my pokemons using v-for.
Do you have any idea why I meet this error ?enter image description here

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2025-02-05 12:28:43

我刚刚将您的代码粘贴到代码笔中,并删除了工作/不工作评论,并且代码运行并显示了名称。

也许问题在于安装此组件的父组件,或者分配:键属性
尝试: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 as PokemonListVue 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文