Skip to content

Commit aded81c

Browse files
authored
Merge pull request #56 from sc0Vu/backward-support
Backward support to vue version < 3.0.0
2 parents 15eba13 + ed9347e commit aded81c

File tree

4 files changed

+96
-89
lines changed

4 files changed

+96
-89
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@ A small wrapper for integrating axios to Vuejs
55

66
|VueJS \ VueAxios|1.x|2.x|3.x|
77
|-|-|-|-|
8-
|1.x|&#10004;|&#10004;|&#10060;|
9-
|2.x|&#10004;|&#10004;|&#10060;|
8+
|1.x|&#10004;|&#10004;|&#10004;|
9+
|2.x|&#10004;|&#10004;|&#10004;|
1010
|3.x|&#10060;|&#10060;|&#10004;|
1111

1212
## How to install:
1313
### ES6 Module:
1414
```bash
1515
npm install --save axios vue-axios
1616
```
17-
18-
And in your entry file:
17+
Import libraries in entry file:
1918
```js
2019
import Vue from 'vue'
2120
import axios from 'axios'
2221
import VueAxios from 'vue-axios'
22+
```
23+
24+
Usage in Vue 2:
25+
```js
26+
Vue.use(VueAxios, axios)
27+
```
2328

29+
Usage in Vue 3:
30+
```js
2431
const app = Vue.createApp(...)
2532
app.use(VueAxios, axios)
2633
```

package-lock.json

Lines changed: 55 additions & 80 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"homepage": "https://github.com/imcvampire/vue-axios#readme",
3131
"dependencies": {
3232
"axios": "^0.20.0",
33-
"vue": "^3.0.0"
33+
"semver": "^7.3.2",
34+
"vue": "^2.0.0"
3435
},
3536
"devDependencies": {
3637
"@babel/core": "^7.11.6",

src/index.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(function () {
2-
2+
const semver = require('semver')
33
/**
44
* Install plugin
55
* @param app
@@ -16,12 +16,36 @@ function plugin(app, axios) {
1616
return
1717
}
1818

19+
if (semver.valid(app.version) == null) {
20+
console.error('Unkown vue version')
21+
return
22+
}
23+
1924
plugin.installed = true;
2025

21-
app.axios = axios;
26+
if (semver.lt(app.version, '3.0.0')) {
27+
Object.defineProperties(app.prototype, {
2228

23-
app.config.globalProperties.axios = axios;
24-
app.config.globalProperties.$http = axios;
29+
axios: {
30+
get: function get() {
31+
return axios;
32+
}
33+
},
34+
35+
$http: {
36+
get: function get() {
37+
return axios;
38+
}
39+
}
40+
41+
});
42+
} else {
43+
app.config.globalProperties.axios = axios;
44+
app.config.globalProperties.$http = axios;
45+
}
46+
47+
app.axios = axios;
48+
app.$http = axios;
2549
}
2650

2751
if (typeof exports == "object") {

0 commit comments

Comments
 (0)