search

Home  >  Q&A  >  body text

Create an API pull for Vue.js

I don't know what's wrong with me or why this isn't working, I just want the random dog photo api to work but it's not working. See the code below.

export default {
  data() {
    return {
      posts: [],
    };
  },

  methods: {
    async getData() {
      try {
        let response = await fetch ("https://dog.ceo/api/breeds/image/random");
        this.posts = await response.json();
      } catch (error){
        console.log(error)
      }
    }
  }
};

P粉111641966P粉111641966258 days ago522

reply all(1)I'll reply

  • P粉300541798

    P粉3005417982024-04-04 00:08:51

    You must call the getData function. One possible solution is that you can call the getData function in the mounted lifecycle hook. This is a vue playground Link

    sssccc
    
    

    reply
    0
  • Cancelreply