The vue plug-in ported by axios — v-axios was
originally intended to allow everyone to introduce the axios plug-in in vue more semantically for more convenient and faster development.
3.0 will support vue 2, 3 at the same time
npm install v-axios --save
import vAxios from 'v-axios' ;
import axios from 'axios' ;
Use Vue 2:
// The second parameter is optional
Vue . use ( VueAxios , axios ? ) ;
Use Vue 3:
import { createApp } from 'vue';
const app = createApp ( ... ) ;
// The second parameter is optional
app . use ( vAxios , axios ? ) ;
<script src="https://unpkg.com/v-axios/dist/v-axios.min.js"></script>
App.axios or this.$http can be called
You should use it like the following:
// get
// es6 写法
Vue.axios.get('xxx').then(res => console.log(res.data));
// es5 is written
Vue . axios . get ( 'xxx' , {
params : {
...
}
}).then(function (res) {
console.log(res);
}).catch(function (err) {
console.log(err);
});
// post
// es6 写法
this.$http.post('xxx').then(res => console.log(res.data)).catch(err => console.log(err)});
// es5 is written
this . $http . post ( 'xxx' , {
...
}).then(function (res) {
console.log(res);
}).catch(function (err) {
console.log(err);
});
See- axios api
Author: xiaofan9
Source Code: https://github.com/xiaofan9/v-axios
#vuejs #vue #javascript