El Hombre Sin Nombre
El Hombre Sin Nombre

Reputation: 3092

Vue - Axios is not defined

In a Laravel+Vue project i trying to use axios to get a API response. Axios call a Laravel endpoint and get controller response.

The code looks

JS

require("./bootstrap");

import Swal from "sweetalert2";
import VueI18n from "vue-i18n";
import VueRouter from "vue-router";
import axios from "axios";

import Vuetify from "vuetify";
import es from "vuetify/es5/locale/es";
import en from "vuetify/es5/locale/en";
import "@mdi/font/css/materialdesignicons.css";

import ContadoresComponent from "./components/ContadorComponent.vue";
import GatewaysComponent from "./components/GatewayComponent.vue";
import MainComponent from "./components/MainComponent.vue";


const routes = [{
        path: "/",
        name: "principal",
        component: MainComponent
    },
    {
        path: "/contadores",
        name: "contadores",
        component: ContadoresComponent
    },
    {
        path: "/gateways",
        name: "gateways",
        component: GatewaysComponent
    }
];

window.Vue = require("vue");
Vue.use(Vuetify);
Vue.use(VueRouter);
Vue.use(VueI18n);
Vue.use(axios);

Vue.component(
    "drawer-component",
    require("./components/DrawerComponent.vue").default
    /*  methods: {
          changeLocale (lang) {
            this.$vuetify.lang.current = lang
          },
    },*/
);

Vue.component(
    "table-component",
    require("./components/TableComponent.vue").default
);

export default new Vuetify({
    icons: {
        iconfont: "mdi"
    },
    lang: {
        locales: {
            es,
            en
        },
        current: "es"
    }
});

const router = new VueRouter({
    routes
});

new Vue({
    vuetify: new Vuetify(),
    router
}).$mount("#app");

Vue (Vuetify)

<template>
  <v-container class="fill-height" fluid>
    <v-row align="center" justify="center">
      <v-card class="mx-auto">
        <v-list-item>
          <v-list-item-content>
            <v-list-item-title class="headline">Contador</v-list-item-title>
          </v-list-item-content>
        </v-list-item>
        <table-component></table-component>
      </v-card>
    </v-row>
  </v-container>
</template>
<script>
axios.get("/contadores").then(response => console.log(response));
</script>

The error: Return axios is not defined, but i think that i defined in App.js file.

Anybody see the error?

Upvotes: 0

Views: 1504

Answers (4)

El Hombre Sin Nombre
El Hombre Sin Nombre

Reputation: 3092

I fix the error with

export default {
  mounted() {
    axios
      .get("ENDPOINT")
      .then(response => console.log(response));
  }
};

Upvotes: 0

Saulo Lins
Saulo Lins

Reputation: 78

An interesting approach that I use in my projects, is to use the library vue-axios, which is very simple to be installed. In case you need to install the dependencies in your project through the commands npm install --save axios vue-axios and then import it into the main.js file or the main file of your application:

import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
 
Vue.use (VueAxios, axios)

The use of axios in the component will be through this.axios.get(`${url}`). Another approach already exposed by friends here at StackOverflow is to import the axios directly into the component you want to use:

<template>
<div>
...
</div>
</template>
<script>
import axios from 'axios';

export default {
   name: 'ComponentName',
   methods: {
      async getAsyncApi() {
         try {
            const result = await axios.get(`${url}`);
        } catch(e) {
            window.console.error('Error! ', e.message);
        }
      },
      getApi() {
         let result;
         axios.get(`${url}`).then((r) => {
           result = r;
         }).catch(e => window.console.error('Error! ', e.message));
      },
   },
};
</script>

Upvotes: 0

Sehdev
Sehdev

Reputation: 5662

You have not imported axios in your file.

To solve this issue either import it in your file where you want to use it as below

import axios from 'axios';

OR

If you don't want to import it in each component where you use it, you can add it to the Vue prototype:

window.axios = require('axios');

//... configure axios...

Vue.prototype.$http = window.axios;

Then in any component you can use

this.$http.post('event/store', {
    'name': this.name,
})

Upvotes: 0

k-code
k-code

Reputation: 104

You still need to import it in the second file. You scrip tag should look like this:

<script>
import axios from 'axios';
axios.get("/contadores").then(response => console.log(response));
</script>

Upvotes: 1

Related Questions