0

Here is my code:

Main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'

Vue.config.productionTip = false
var eventBus = new Vue();
Vue.prototype.$eventBus = eventBus;
Vue.prototype.$axios = axios;

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

Calling API

methods:{
    loginMethode(){
        console.log(this.user);
        this.$eventBus.$emit("loadingStatus",true);
        this.$axios.get('https://api.coindesk.com/v1/bpi/currentprice.json')
        .then(res=>{
            console.log(res);
        });
        this.$eventBus.$emit("loadingStatus",false);
    }
  }

It's showing an error message that

Error in v-on handler: "TypeError: Cannot read property 'get' of undefined"

1 Answer 1

1

You see the error, because "$axios" object that you define inside a Main.js module is not defined inside your ./App.js module. You should pass it there somehow. Consider to use vue mixins or component extention or composition API.

Sign up to request clarification or add additional context in comments.

3 Comments

How to pass ? Give me an example please.
The simplest way is to import axios inside App.js: ` import axios from 'axios'; ` and then using it as a regular module: ` loginMethode(){ ... axios.get('api.coindesk.com/v1/bpi/currentprice.json') .then(res=>{ console.log(res); }); ... } `
I am using axios from my Login.vue file. How to import in .vue file ? Ihave updated my full code from where I am using this axios.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.