Skip to content

Instantly share code, notes, and snippets.

@mindmergedesign
Forked from unr/main.js
Created September 14, 2017 16:12
Show Gist options
  • Save mindmergedesign/5d1ea23ca58c477df81827623b627522 to your computer and use it in GitHub Desktop.
Save mindmergedesign/5d1ea23ca58c477df81827623b627522 to your computer and use it in GitHub Desktop.
Simplistic Axios in VueJs Example
const axiosconfig = {
baseurl: 'https://site.com/api/',
timeout: 30000,
};
import Vue from 'vue';
import axios from 'axios';
// Setting up Axios on Vue Instance, for use via this.$axios
Vue.prototype.$axios = axios.create(axiosConfig);
// Default vars set up from localStorage (ie, user has come back)
Vue.prototype.$axios.defaults.headers.common.Authorization = `Bearer ${localStorage.getItem('id_token')}`;
Vue.prototype.$axios.defaults.headers.common['Access-Token'] = localStorage.getItem('auth_token');
// Example app starts up, uses axios to collect posts.
new Vue({
mounted() {
this.$axios.get('/posts').then((response) => {
this.posts = response.data;
}).catch((err) => {
console.log(err);
});
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment