Complete Vue.js 2 Web Development_ Practical guide to building end-to-end web development solutions with Vue.js 2

(singke) #1
Vue Communicates with the Internet Chapter 14

Our Vue instance is as follows:


new Vue({
el: '#app',
data: {
advice: 'loading...'
},
created () {
axios.get('http://api.adviceslip.com/advice')
.then(response => {
this.advice = response.data.slip.advice
})
.catch(error => {
this.advice = 'There was an error: ' + error.message
})
}
})

Open your app to have a refreshingly wise slip of advice:


How it works...


When our application starts up, the created hook is engaged and will run the code with


Axios. The first line performs a GET request to the API endpoint:


axios.get('http://api.adviceslip.com/advice')
Free download pdf