import/use axios or any other libraries in Vue components

I used to do this in main.js:

import axios from 'axios'

window.axios = axios

and use window.axios in components.

This forum reply suggested a much better solution:

// main.js
import Vue from 'vue';
import axios from 'axios';

Vue.prototype.$http = axios; // or $axios or whatever you want

// component.js
// …
methods: {
getItems () {
// …


Leave a Reply

Your email address will not be published. Required fields are marked *