UNPKG

3.42 kBMarkdownView Raw
1# vue-axios
2
3[![npm version](https://img.shields.io/npm/v/vue-axios.svg?style=flat-square)](https://www.npmjs.org/package/vue-axios)
4[![install size](https://packagephobia.now.sh/badge?p=vue-axios)](https://packagephobia.now.sh/result?p=vue-axios)
5[![npm downloads](https://img.shields.io/npm/dm/vue-axios.svg?style=flat-square)](http://npm-stat.com/charts.html?package=vue-axios)
6[![jsdelivr](https://data.jsdelivr.com/v1/package/npm/vue-axios/badge?style=rounded)](https://www.jsdelivr.com/package/npm/vue-axios)
7[![License](https://img.shields.io/npm/l/vue-axios.svg)](https://www.npmjs.com/package/vue-axios)
8
9A small wrapper for integrating axios to Vuejs
10
11## Why
12
13I created this library because, in the past, I needed a simple solution to migrate from `vue-resource` to `axios`.
14
15It only binds axios to the `vue` instance so you don't have to import everytime you use `axios`.
16
17## Support matrix
18
19| VueJS \ VueAxios | 1.x | 2.x | 3.x |
20| ---------------- | -------- | -------- | -------- |
21| 1.x | ✔ | ✔ | ✔ |
22| 2.x | ✔ | ✔ | ✔ |
23| 3.x | ❌ | ❌ | ✔ |
24
25## How to install:
26### ES6 Module:
27```bash
28npm install --save axios vue-axios
29```
30Import libraries in entry file:
31```js
32// import Vue from 'vue' // in Vue 2
33import * as Vue from 'vue' // in Vue 3
34import axios from 'axios'
35import VueAxios from 'vue-axios'
36```
37
38Usage in Vue 2:
39```js
40Vue.use(VueAxios, axios)
41```
42
43Usage in Vue 3:
44```js
45const app = Vue.createApp(...)
46app.use(VueAxios, axios)
47```
48
49### Script:
50Just add 3 scripts in order: `vue`, `axios` and `vue-axios` to your `document`.
51
52## Usage:
53
54### in Vue 2
55
56This wrapper bind `axios` to `Vue` or `this` if you're using single file component.
57
58You can use `axios` like this:
59```js
60Vue.axios.get(api).then((response) => {
61 console.log(response.data)
62})
63
64this.axios.get(api).then((response) => {
65 console.log(response.data)
66})
67
68this.$http.get(api).then((response) => {
69 console.log(response.data)
70})
71```
72
73### in Vue 3
74
75This wrapper bind `axios` to `app` instance or `this` if you're using single file component.
76
77in option API, you can use `axios` like this:
78
79```js
80// App.vue
81export default {
82 name: 'App',
83 methods: {
84 getList() {
85 this.axios.get(api).then((response) => {
86 console.log(response.data)
87 })
88 // or
89 this.$http.get(api).then((response) => {
90 console.log(response.data)
91 })
92 }
93 }
94}
95```
96
97however, in composition API `setup` we can't use `this`, you should use `provide` API to share the globally instance properties first, then use `inject` API to inject `axios` to `setup`:
98
99```js
100// main.ts
101import { createApp } from 'vue'
102import App from './App.vue'
103import store from './store'
104import axios from 'axios'
105import VueAxios from 'vue-axios'
106
107const app = createApp(App).use(store)
108app.use(VueAxios, axios)
109app.provide('axios', app.config.globalProperties.axios) // provide 'axios'
110app.mount('#app')
111
112// App.vue
113import { inject } from 'vue'
114
115export default {
116 name: 'Comp',
117 setup() {
118 const axios: any = inject('axios') // inject axios
119
120 const getList = (): void => {
121 axios
122 .get(api)
123 .then((response: { data: any }) => {
124 console.log(response.data)
125 });
126 };
127
128 return { getList }
129 }
130}
131```
132
133Please kindly check full documention of [axios](https://github.com/axios/axios) too