Skip to content

Commit 282e114

Browse files
committed
axios configuration finished
1 parent a3000f1 commit 282e114

File tree

10 files changed

+86
-35
lines changed

10 files changed

+86
-35
lines changed

.env.development

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
VUE_APP_DEVELOPMENT=DEVELOPMENT
2-
VUE_APP_MODE=DEVELOPMENT
1+
VUE_APP_API=https://vitejs-vite-t9mch8--3001.local.webcontainer.io/

.env.production

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
VUE_APP_PRODUCTION=PRODUCTION
2-
VUE_APP_MODE=PRODUCTION

README.md

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1 @@
1-
# vue3-router
2-
3-
## Project setup
4-
```
5-
npm install
6-
```
7-
8-
### Compiles and hot-reloads for development
9-
```
10-
npm run serve
11-
```
12-
13-
### Compiles and minifies for production
14-
```
15-
npm run build
16-
```
17-
18-
### Lints and fixes files
19-
```
20-
npm run lint
21-
```
22-
23-
### Customize configuration
24-
See [Configuration Reference](https://cli.vuejs.org/config/).
1+
Working...

db.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"posts": [
3+
{
4+
"id": 1,
5+
"title": "json-server",
6+
"author": "typicode"
7+
}
8+
],
9+
"comments": [
10+
{
11+
"id": 1,
12+
"body": "some comment",
13+
"postId": 1
14+
}
15+
],
16+
"profile": {
17+
"name": "typicode"
18+
}
19+
}

document.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ https://vuejs.org/guide/essentials/component-basics.html#dynamic-components
22
https://vuejs.org/guide/built-ins/transition.html#javascript-hooks
33
https://vuejs.org/guide/built-ins/keep-alive.html
44
https://router.vuejs.org/guide/advanced/composition-api.html#accessing-the-router-and-current-route-inside-setup
5-
https://router.vuejs.org/guide/advanced/lazy-loading.html#with-vite
5+
https://router.vuejs.org/guide/advanced/lazy-loading.html#with-vite
6+
7+
https://skirtles-code.github.io/vue-examples/patterns/global-properties.html

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
"build": "vue-cli-service build",
88
"lint": "vue-cli-service lint",
99
"test:unit": "vue-cli-service test:unit",
10-
"json-server": "json-server --w --p 3002 ./db/db.json"
10+
"frontend": "http-server",
11+
"backend": "json-server -w db.json"
1112
},
1213
"dependencies": {
14+
"axios": "^1.1.2",
1315
"core-js": "^3.8.3",
1416
"vue": "^3.2.13",
1517
"vue-router": "^4.0.13"

src/App.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@
88
<router-view></router-view>
99
</template>
1010
<script>
11+
import { useHttp } from '@/services/http.js';
1112
export default {
1213
name: 'MyApp',
14+
setup() {
15+
const http = useHttp();
16+
17+
async function fetchData() {
18+
const response = await http.get('/posts/');
19+
console.log(response);
20+
}
21+
return {
22+
fetchData,
23+
};
24+
},
25+
created() {
26+
this.fetchData();
27+
},
1328
};
1429
</script>

src/main.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { createApp } from 'vue';
22

3-
import router from './router';
4-
import './main.css';
5-
63
import App from './App.vue';
74
import QButton from '@/components/QButton.vue';
85

6+
import './main.css';
7+
import router from './router';
8+
import { plugin } from '@/services/http.js';
9+
910
const app = createApp(App);
1011

1112
app.use(router);
13+
app.use(plugin);
14+
1215
app.component('q-button', QButton);
13-
app.mount('#app');
1416

15-
// createApp(App).use(router).mount('#app');
17+
app.mount('#app');

src/services/http.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { inject } from 'vue';
2+
import axios from 'axios';
3+
4+
const baseURL =
5+
process.env.NODE_ENV === 'development' ? process.env.VUE_APP_API : '';
6+
const injectionKey = Symbol('$http');
7+
8+
export const useHttp = () => inject(injectionKey);
9+
10+
export const plugin = {
11+
install(app) {
12+
const instance = axios.create({
13+
baseURL,
14+
});
15+
instance.interceptors.request.use(
16+
function (config) {
17+
return config;
18+
},
19+
function (error) {
20+
return Promise.reject(error);
21+
}
22+
);
23+
24+
instance.interceptors.response.use(
25+
function (response) {
26+
return response;
27+
},
28+
function (error) {
29+
return Promise.reject(error);
30+
}
31+
);
32+
app.provide(injectionKey, instance);
33+
34+
app.config.globalProperties.$http = instance;
35+
},
36+
};

src/untils/example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.

0 commit comments

Comments
 (0)