如何在Vue.js 3中从Laravel响应API中获取每个数据

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

how to get each data from response api laravel in vue js 3

问题

需要帮助,我可以从响应 API 中获取所有数据,但尝试从键“get_item_cards” 中获取数据(循环数据)时出现了一些问题。这是我的响应和 Vue.js 代码

响应 API

<script setup>
<script>
import axios from 'axios';

export default {
    name: 'ListNotes',
    data() {
        return {
            cardNotes: [],
        }
    },
    mounted() {
        // console.log('Page mounted');
        this.getListNotes();
    },
    methods: {
        getListNotes() {
            axios.get('http://localhost:8000/api/card').then(res => {
                this.cardNotes = res.data.cardNotes;
                console.log(this.cardNotes);
            })
        }
    }
}
</script>

在 Vue.js 3 中,从关系中获取所有数据和每个数据的最佳方法是什么?

英文:

Need help , i can get all data from response api but having some problem when try to get data (looping data ) from key "get_item_cards" . Here's my response and code in vue js

Response api

&lt;script setup&gt;
&lt;script&gt;
import axios from &#39;axios&#39;

export default {
    name: &#39;ListNotes&#39;,
    data() {
        return {
            cardNotes: [],
           
        }
    },
    mounted() {
        // console.log(&#39;Page mounted&#39;);
        this.getListNotes();
    },
    methods: {
        getListNotes() {
            axios.get(&#39;http://localhost:8000/api/card&#39;).then(res =&gt; {
                this.cardNotes = res.data.cardNotes
                console.log(this.cardNotes);

         
            })
        }
    }
}
&lt;/script&gt;

how the best way to get all data & each data from relationship in vue js 3

答案1

得分: 0

由于this.cardNote返回一个包含三个元素的数组,您可以使用v-for循环并像下面这样访问get_item_cards数组,

<template>
  <div>
       <div v-for="(note, index) in cardNote">
           <p>{{note.cardname}}</p>
           <div v-for="(item, key) in note.get_item_cards">
               <p>{{item.content}}</p>
           </div>
       </div>
  </div>
</template>
<script setup>
<script>
import axios from 'axios';

export default {
    name: 'ListNotes',
    data() {
        return {
            cardNotes: [],
        }
    },
    mounted() {
        this.getListNotes();
    },
    methods: {
        getListNotes() {
            axios.get('http://localhost:8000/api/card').then(res => {
                this.cardNotes = res.data.cardNotes;
                console.log(this.cardNotes);
            })
        }
    }
}
</script>
英文:

Since the this.cardNote returns an array with three elements, you can use loop using v-for and access to the get_item_cards array like below,

&lt;template&gt;
  &lt;div&gt;
       &lt;div v-for=(note, index) in cardNote&gt;
           &lt;p&gt;{{node.cardname}}&lt;/p&gt;
           &lt;div v-for=(item, key) in note.get_item_cards&gt;
               &lt;p&gt;{{item.content}}&lt;/p&gt;
           &lt;/div&gt;
           
       &lt;/div&gt;
  &lt;/div&gt;
&lt;/template&gt;
&lt;script setup&gt;
&lt;script&gt;
import axios from &#39;axios&#39;

export default {
    name: &#39;ListNotes&#39;,
    data() {
        return {
            cardNotes: [],
           
        }
    },
    mounted() {
        // console.log(&#39;Page mounted&#39;);
        this.getListNotes();
    },
    methods: {
        getListNotes() {
            axios.get(&#39;http://localhost:8000/api/card&#39;).then(res =&gt; {
                this.cardNotes = res.data.cardNotes
                console.log(this.cardNotes);

         
            })
        }
    }
}
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年2月16日 13:17:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468108.html
匿名

发表评论

匿名网友

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

确定