[Vuejs]-How to pass function parameters in Vue to make a GET request using axios?

0๐Ÿ‘

โœ…

You can use the mounted() function of the Vue instance, it will be triggered as soon as the component is mounted

cf: https://v2.vuejs.org/v2/api/#mounted

for example:

<tr v-for="(cabinet, i) in cabinets">
    <td>{{ cabinet }}</td>
</tr>
<script>
const axios = require('axios');

export default {
  name: "Cabinets",
  data() {
    return {
      cabinets: []
    }
  },
  mounted() {
    axios({
      method: 'GET',
      // Use backticks to do string interpolation
      // Replace the id variable by the correct id definition (idk where you get it)
      url: `user/${this.$session.get('user').id}/cabinets`
    }).then(response => {
      this.cabinets = response.data
    })
  }
}
</script>

0๐Ÿ‘

You can get the response using axios.

    axios.get('user/${$id}/cabinets')
      .then(response => {
        console.log(response)
        this.cabinets = response.data
    })

Leave a comment