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 |
|
9 | A small wrapper for integrating axios to Vuejs
|
10 |
|
11 | ## Why
|
12 |
|
13 | I created this library because, in the past, I needed a simple solution to migrate from `vue-resource` to `axios`.
|
14 |
|
15 | It 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
|
28 | npm install --save axios vue-axios
|
29 | ```
|
30 | Import libraries in entry file:
|
31 | ```js
|
32 | // import Vue from 'vue' // in Vue 2
|
33 | import * as Vue from 'vue' // in Vue 3
|
34 | import axios from 'axios'
|
35 | import VueAxios from 'vue-axios'
|
36 | ```
|
37 |
|
38 | Usage in Vue 2:
|
39 | ```js
|
40 | Vue.use(VueAxios, axios)
|
41 | ```
|
42 |
|
43 | Usage in Vue 3:
|
44 | ```js
|
45 | const app = Vue.createApp(...)
|
46 | app.use(VueAxios, axios)
|
47 | ```
|
48 |
|
49 | ### Script:
|
50 | Just add 3 scripts in order: `vue`, `axios` and `vue-axios` to your `document`.
|
51 |
|
52 | ## Usage:
|
53 |
|
54 | ### in Vue 2
|
55 |
|
56 | This wrapper bind `axios` to `Vue` or `this` if you're using single file component.
|
57 |
|
58 | You can use `axios` like this:
|
59 | ```js
|
60 | Vue.axios.get(api).then((response) => {
|
61 | console.log(response.data)
|
62 | })
|
63 |
|
64 | this.axios.get(api).then((response) => {
|
65 | console.log(response.data)
|
66 | })
|
67 |
|
68 | this.$http.get(api).then((response) => {
|
69 | console.log(response.data)
|
70 | })
|
71 | ```
|
72 |
|
73 | ### in Vue 3
|
74 |
|
75 | This wrapper bind `axios` to `app` instance or `this` if you're using single file component.
|
76 |
|
77 | in option API, you can use `axios` like this:
|
78 |
|
79 | ```js
|
80 | // App.vue
|
81 | export 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 |
|
97 | however, 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 | ```ts
|
100 | // main.ts
|
101 | import { createApp } from 'vue'
|
102 | import App from './App.vue'
|
103 | import store from './store'
|
104 | import axios from 'axios'
|
105 | import VueAxios from 'vue-axios'
|
106 |
|
107 | const app = createApp(App).use(store)
|
108 | app.use(VueAxios, axios)
|
109 | app.provide('axios', app.config.globalProperties.axios) // provide 'axios'
|
110 | app.mount('#app')
|
111 |
|
112 | // App.vue
|
113 | import { inject } from 'vue'
|
114 |
|
115 | export 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 |
|
133 | Please kindly check full documentation of [axios](https://github.com/axios/axios) too
|
134 |
|
135 | ## Multiple axios instances support
|
136 |
|
137 | The library allows to have different version of axios at the same time as well as change the default registration names (e.g. `axios` and `$http`). To use this feature you need to provide options like an object where:
|
138 | - `<key>` is registration name
|
139 | - `<value>` is instance of axios
|
140 |
|
141 | For Vue it looks like this:
|
142 | ```js
|
143 | // Vue 2 / Vue 3 + Composition API
|
144 | import App from './App.vue'
|
145 | import VueAxios from 'vue-axios'
|
146 | import axios from 'axios'
|
147 | import axios2 from 'axios'
|
148 | Vue.use(VueAxios, { $myHttp: axios, axios2: axios2 }) // or app.use() for Vue 3 Optiona API
|
149 |
|
150 | // usage
|
151 | export default {
|
152 | methods: {
|
153 | getList(){
|
154 | this.$myHttp.get(api).then((response) => {
|
155 | console.log(response.data)
|
156 | })
|
157 | this.axios2.get(api).then((response) => {
|
158 | console.log(response.data)
|
159 | })
|
160 | }
|
161 | }
|
162 | }
|
163 | ```
|
164 | It works similarly in Options API of Vue 3 but if you want to use Composition API you should adjust your code a bit to extract proper keys, e.g.:
|
165 | ```ts
|
166 | // Vue 2 + Setup function
|
167 | import { createApp } from 'vue'
|
168 | import App from './App.vue'
|
169 | import store from './store'
|
170 | import axios from 'axios'
|
171 | import VueAxios from 'vue-axios'
|
172 |
|
173 | const app = createApp(App).use(store)
|
174 | app.use(VueAxios, { $myHttp: axios, axios2: axios2 })
|
175 | app.provide('$myHttp', app.config.globalProperties.$myHttp) // provide '$myHttp'
|
176 | app.provide('axios2', app.config.globalProperties.axios2) // provide 'axios2'
|
177 | app.mount('#app')
|
178 |
|
179 | // App.vue
|
180 | import { inject } from 'vue'
|
181 |
|
182 | export default {
|
183 | name: 'Comp',
|
184 | setup() {
|
185 | const $myHttp: any = inject('$myHttp') // inject $myHttp
|
186 |
|
187 | const getListWithMyHttp = (): void => {
|
188 | $myHttp
|
189 | .get(api)
|
190 | .then((response: { data: any }) => {
|
191 | console.log(response.data)
|
192 | });
|
193 | };
|
194 |
|
195 | const axios2: any = inject('axios2') // inject axios2
|
196 | const getListWithAxios2 = (): void => {
|
197 | axios2
|
198 | .get(api)
|
199 | .then((response: { data: any }) => {
|
200 | console.log(response.data)
|
201 | });
|
202 | };
|
203 |
|
204 |
|
205 | return { getListWithMyHttp, getListWithAxios2 }
|
206 | }
|
207 | }
|
208 | ```
|