E
E
enginel2021-03-05 01:31:22
Vue.js
enginel, 2021-03-05 01:31:22

How to output data using axios in Vue?

Hello, I'm learning vue. Tell me how, when clicking on a button, unload the users array from json in class = "allUsers"? I see in the console that data is loaded when clicked, but how can I transfer them to a div after that?
Is it good if get is triggered with every next click on the button? Or is it considered bad practice?

<div class="allUsers" v-for="user of users" :key="user">
      <h2>{{user .name}}</h2>
      <p>{{user .email}}</p>
</div>

 <button @click="loadUsers">Click</button>


<script>
export default {
data: () => ({
    users: [],
}), 
methods: {
  loadUsers(){
   this.$axios.$get('https://jsonplaceholder.typicode.com/users');
  }
}
}
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Levchenko, 2021-03-05
@enginel

this.$axios.$get('https://jsonplaceholder.typicode.com/users').then((res) => {
  console.log(res);
  this.users = res.data;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question