什么可能导致我的 Vue.js fetch API 代码未按预期显示虚拟用户?

huangapple go评论73阅读模式
英文:

What could be causing my Vue.js fetch API code to not display fake users as expected?

问题

Here is the translated code portion:

<body class="container bg-warning">
    <h1>
        Test Users via REST
    </h1>
    <p>
        Reference to the site reqres.in responsible for providing test data,
        in JSON format, for free.
    </p>
    <div id="usuarios" class="container-fluid">
        <div class="row">
            <div class="card col-3 p-2 m-3" v-for="(user,index) in users" v-bind:key="index">
                <div class="card-body">
                    <img class="card-img-top" :src="user.avatar">
                    </img>
                    <h1>
                        {{ user.first_name }} + {{ user.last_name }}
                    </h1>
                    <p>
                        {{ user.email }}
                    </p>
                </div>
            </div>
        </div>
    </div>
    <script>
        const { createApp } = Vue

        createApp({
            data() {
                return {
                    users: []
                }
            },
            methods: {
                loadUsers() {
                    fetch('https://reqres.in/api/users?per_page=10').then(res => res.json()).then(data => this.users = data.data);
                }
            },
            mounted() {
                this.loadUsers()
            }
        }).mount('#usuarios')
    </script>
</body>
</html>

Please note that I've translated the code while keeping the structure and placeholders intact.

英文:
&lt;body class=&quot;container bg-warning&quot;&gt;
    &lt;h1&gt;
        Usu&#225;rios de Teste via REST
    &lt;/h1&gt;
    &lt;p&gt;
        refer&#234;ncia ao site reqres.in respons&#225;vel por fornecer os dados de teste,
        no formato JSON, de forma gratuita
    &lt;/p&gt;
    &lt;div id=&quot;usuarios&quot; class=&quot; container-fluid&quot;&gt;
        &lt;div class=&quot;row&quot;&gt;
            &lt;div class=&quot;card col-3 p-2 m-3&quot; v-for=&quot;(user,index) in users&quot; v-bind:key=&quot;index&quot;&gt;
                &lt;div class=&quot; card-body&quot;&gt;
                    &lt;img class=&quot;card-img-top&quot; src={{user.avatar}}&gt;
                    &lt;/img&gt;
                    &lt;h1&gt;
                        {{user.first_name}} + {{user.last_name}}
                    &lt;/h1&gt;
                    &lt;p&gt;
                        {{user.email}}
                    &lt;/p&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;script&gt;
        const {
            createApp
        } = Vue

        createApp({
            data() {
                return {
                    users: []
                }
            },
            methods: {
                loadUsers() {
                    fetch(&#39;https://reqres.in/api/users?per_page=10&#39;).then(res =&gt;res.json()).then(data =&gt;users = (data.data));

                }
            },
            mounted() {

                this.loadUsers()
            }
        },
        ).mount(&#39;#usuarios&#39;)
    &lt;/script&gt;
&lt;/body&gt;

</html>"
//i tried changing the method loadUsers multiple times. this is the way that dont show errors .
//however still does not work. the goal is to load the array users with the data from the fetch. and use the user in the vue to show the information from users

答案1

得分: 0

在组件实例内部,您需要使用 this 引用数据和方法(在HTML部分不需要,因为Vue很智能!)

then(data => this.users = (data.data));

确保您在代码中使用此脚本标签来使用来自CDN的Vue(我假设您已经有它,但它没有在您的代码中!)

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

就像官方文档中的示例一样:

英文:

Inside the component instance you need to references the data and methods by using this
(it's not needed in the HTML part because Vue is clever!)

then(data =&gt; this.users = (data.data));

Make sure you have this script tag to use Vue from a CDN (I assume you have it but it's not on your code!)
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
Like in the example on the official doc:

huangapple
  • 本文由 发表于 2023年5月26日 07:22:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76336782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定