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: https://forum.vuejs.org/t/importing-same-objects-inside-components-and-app/12812/2?u=lookrain

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

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

// component.js
// …
methods: {
getItems () {
this.$http.get(‘/items/’)
// …
“`

Nice!

Leave a Reply

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