UNPKG

5.5 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```ts
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 documentation of [axios](https://github.com/axios/axios) too
134
135## Multiple axios instances support
136
137The 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
141For Vue it looks like this:
142```js
143// Vue 2 / Vue 3 + Composition API
144import App from './App.vue'
145import VueAxios from 'vue-axios'
146import axios from 'axios'
147import axios2 from 'axios'
148Vue.use(VueAxios, { $myHttp: axios, axios2: axios2 }) // or app.use() for Vue 3 Optiona API
149
150// usage
151export 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```
164It 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
167import { createApp } from 'vue'
168import App from './App.vue'
169import store from './store'
170import axios from 'axios'
171import VueAxios from 'vue-axios'
172
173const app = createApp(App).use(store)
174app.use(VueAxios, { $myHttp: axios, axios2: axios2 })
175app.provide('$myHttp', app.config.globalProperties.$myHttp) // provide '$myHttp'
176app.provide('axios2', app.config.globalProperties.axios2) // provide 'axios2'
177app.mount('#app')
178
179// App.vue
180import { inject } from 'vue'
181
182export 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```