wDr*_*rik 5 async-await axios vuejs2
我有以下代码,并想知道如何使用执行相同功能的async/await实现try/catch:
import Vue from 'vue'
import axios from 'axios'
new Vue({
el: '#app',
data: {
skills: [],
},
mounted() {
axios
.get('http://localhost:8080/wp-json/api/v1/skills')
.then(response => {
this.skills = response
}).catch(err => (console.log(err)))
}
})
Run Code Online (Sandbox Code Playgroud)
谢谢!
jac*_*cky 12
见下面的代码:
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)
}
}
})Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)