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 | ```js
|
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 documention of [axios](https://github.com/axios/axios) too
|