How to use async / await in get request using vue + axios?

后端 未结 1 1104
醉酒成梦
醉酒成梦 2021-02-06 08:32

I have the following code and would like to know how I can implement a try / catch with async / await executing the same function:

import Vue from \'vue\'
import         


        
相关标签:
1条回答
  • 2021-02-06 08:55

    see code below:

    var app = new Vue({
      el: '#app',
      async mounted() {
        try{
          let response = await axios.get('http://localhost:8080/wp-json/api/v1/skills')
          this.skills = response
        }catch(err){
          console.log(err)
        }
      }
    })
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    
    <div id="app">
    </div>

    0 讨论(0)
提交回复
热议问题