Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
How to get response from an APi call in Vuejs?

import axios from "axios";

export const routerid = async (itemId) =>
  await axios.get("https://fakestoreapi.com/products?limit=" + itemId);

<template>
  <div>
    <div v-for="(item, key) in user" :key="key">
      {{ item.price }} <br />
      {{ item.description }} <br />
    </div>
  </div>
</template>

<script>
import { routerid } from "./routerid";
export default {
  name: "User",
  components: {},
  data() {
    return {
      lists: [],
    };
  },
  mounted() {
    if (this.$route.params.id)
      routerid(this.$route.params.id).then((r) => {
        let obj = r.data;
        this.lists = [{ ...obj }];
      });
  },
  computed: {
    user: function () {
      return this.lists.filter((item) => {
        return item.id === this.$route.params.id;
      });
    },
  },
};
</script>

And this is my complete code:- https://codesandbox.io/s/late-brook-eg51y3?file=/src/components/routerid.js

Above is my api call, with url query like url...../?limit=" + id

Above is the logic , which i tried. But not sure whats wrong with code. getting blank screen.

please provide some suggestions, on how to call. and please go through my code once, if there is any other issues. Thanks

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It's all about spread operator, you should spread object inside array correctly, below example works fine.

<template>
  <div>
    <div v-for="(item, key) in user" :key="key">
      {{ item.price }} <br />
      {{ item.description }} <br />
    </div>
  </div>
</template>

<script>
import { routerid } from "./routerid";
export default {
  name: "User",
  components: {},
  data() {
    return {
      lists: [],
    };
  },
  mounted() {
    if (this.$route.params.id)
      routerid(this.$route.params.id).then((r) => {

        let obj = r.data;
        //changed from [{...obj}] to [...obj]
        this.lists = [...obj];
      });
  },
  computed: {
    user: function () {
      return this.lists.filter((item) => {
        return item.id === this.$route.params.id;
      });
    },
  },
};
</script>

about 4 years ago · Juan Pablo Isaza Report

0

You have 2 problems.

1 - Firstly you're using user instead of lists in the for loop.

2 - Secondly you're using spread operator on the retuned data which is already an array so you don't need to do that.

<template>
  <div>
    <div v-for="(item, key) in lists" :key="key">
      {{ item.price }} <br />
      {{ item.description }} <br />
    </div>
  </div>
</template>

<script>
import { routerid } from "./routerid";
export default {
  name: "User",
  components: {},
  data() {
    return {
      lists: [],
    };
  },
  mounted() {
    if (this.$route.params.id)
      routerid(this.$route.params.id).then((r) => {
        this.lists = r.data;
      });
  },
  computed: {
    user: function () {
      return this.lists.filter((item) => {
        return item.id === this.$route.params.id;
      });
    },
  },
};
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!