Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2fa1343

Browse files
author
kevin
committed
feat: add axios for http request
1 parente8f1164 commit2fa1343

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-1
lines changed

‎package-lock.json

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"prepare":"husky install"
1111
},
1212
"dependencies": {
13+
"axios":"^0.26.1",
1314
"vue":"^3.2.25",
1415
"vue-router":"^4.0.14"
1516
},

‎src/api/manager.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
importservicefrom'./service'
2+
3+
exportconstget=(url,params)=>{
4+
returnservice.requese({url:url,params:params,method:'GET'})
5+
}
6+
7+
exportconstpost=(url,data)=>{
8+
returnservice.request({url:url,data:data,method:'POST'})
9+
}

‎src/api/service.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
importaxiosfrom'axios'
2+
3+
constbaseURL=import.meta.env.VITE_API_BASE_URL
4+
5+
constservice=axios.create({
6+
baseURL:baseURL,
7+
timeout:10*60*1000
8+
})
9+
10+
service.interceptors.request.use(
11+
(config)=>{
12+
// 在请求之前配置header
13+
returnconfig
14+
},
15+
(error)=>{
16+
returnPromise.reject(error)
17+
}
18+
)
19+
20+
service.interceptors.response.use(
21+
(response)=>{
22+
const{ status, statusText, data}=response
23+
if(status===200){
24+
returndata
25+
}else{
26+
returnPromise.reject(statusText)
27+
}
28+
},
29+
(error)=>{
30+
returnPromise.reject(error)
31+
}
32+
)
33+
34+
exportdefaultservice

‎src/api/user.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import{post,get}from'./manager'
2+
3+
exportconstinfo=(params)=>get('/user/info',params)
4+
exportconstlogin=(data)=>post('/user/login',data)

‎src/pages/Home.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
</template>
44

55
<script>
6+
import {ref,onBeforeMount }from'vue'
7+
import {info }from'../api/user'
8+
69
exportdefault {
710
setup() {
8-
return {}
11+
constuserInfo=ref(null)
12+
onBeforeMount(async ()=> {
13+
constres=awaitinfo({ id:1 })
14+
userInfo.value= res
15+
})
16+
return {
17+
userInfo
18+
}
919
}
1020
}
1121
</script>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp