Skip to content Skip to sidebar Skip to footer

Vue Composition Api Use Vueaxios?

I am in main.js importing vue-axios main.js import { createApp } from 'vue'; import axios from 'axios'; import VueAxios from 'vue-axios'; import App from './App.vue'; const app =

Solution 1:

generally I'd do something like this ( except I modularize all the api calling into a "service" rather than inlining it into the view code.)

import axios from'axios';
import {onMounted} from'vue'exportdefault {
  name: 'App',
  setup() {

   onMounted(async () => {
     let res = await axios.get('xxxx')
     console.log(res)
   });
  }
};

Solution 2:

This wouldn't work in any version of Vue without an import:

axios.get(...)

Even in Vue 2, you have to use one of these if you use vue-axios:

this.axios.get(...)
this.$http.get(...)
Vue.axios.get(...)

The composition API has no this access in the component, so vue-axios doesn't help much.

Post a Comment for "Vue Composition Api Use Vueaxios?"