Skip to content

Instantly share code, notes, and snippets.

@Gregg
Last active August 16, 2018 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gregg/a867b48e3ef62d5581bcadd3b0682cca to your computer and use it in GitHub Desktop.
Save Gregg/a867b48e3ef62d5581bcadd3b0682cca to your computer and use it in GitHub Desktop.
Axios Service
<template>
<div>
<h1>Events Listing</h1>
<EventCard v-for="event in events" :key="event.id" :event="event"/>
</div>
</template>
<script>
import EventCard from '@/components/EventCard.vue'
import EventService from '@/services/EventService.js'
export default {
components: {
EventCard
},
data() {
return {
events: []
}
},
mounted() {
EventService.getEvents()
.then(response => {
this.events = response.data
})
.catch(error => {
console.log('There was an error:', error.response)
})
}
}
</script>
import axios from 'axios'
export default {
getEvents() {
return this.api().get('/events')
},
api() {
return axios.create({
baseURL: `http://localhost:3000`,
withCredentials: false,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment