axios在Vue中的簡單應用(一)


1、安裝axios:

npm install --save axios vue-axios

2、安裝qs:

qs.stringify(data)可以解決data數據格式問題

npm install --save axios vue-axios qs

3、在main.js頁面中引用:

1 import Vue from 'vue'
2 import axios from 'axios'
3 import qs from 'qs'
4 
5 Vue.prototype.$http = axios
6 Vue.prototype.qs = qs

4、在vue中使用

 1 <script>
 2 export default{
 3     data(){
 4         return {
 5             msg:'axios使用'
 6         }
 7     },
 8     created(){
 9         this.axios({
10                 method:'post',
11                 url:'',
12                 data:this.qs.stringify({
13                     msg:this.msg
14             })
15         }).then((response)=>{
16             console.log(response)
17         })
18     }
19 }
20 </script>                            

以上是axios在vue中的簡單應用,在實際的項目中,我們還需要考慮請求超時、是否登錄等問題,這時需要在http請求中添加攔截器,在請求頭中加token

下面是一個axios的工具interceptAxios.js

 1 //http配置
 2 //引入axios以及element ui 中的loading和message組件
 3 import axios from 'axios'
 4 import store from '../../store/store'
 5 import * as types from '../../store/types'
 6 import router from '../../routes'
 7 import {Loading,Message} from 'element-ui'
 8 //超時時間
 9 axios.defaults.timeout = 500000
10 //http請求攔截器,在請求頭中加token
11 var loadinginstace
12 axios.interceptors.request.use(config=>{
13     if (store.state.token) {
14       config.headers.Authorization = `${store.state.token}`
15     }
16     //element ui Loading方法
17     //loadinginstace = Loading.service({fullscreen:true})
18     return config
19 },error=>{
20     //loadinginstace.close()
21     Message.error({
22         message:'加載超時'
23     })
24     return Promise.reject(error)
25 })
26 //http響應攔截器
27 axios.interceptors.response.use(response=>{//響應成功關閉loading
28     //loadinginstace.close()
29     return response
30 },error=>{
31     if (error.response) {
32       switch (error.response.status) {
33         case 401:
34           // 401 清除token信息並跳轉到登錄頁面
35           store.commit(types.LOGOUT)
36           store.commit(delPermission)
37           console.log("token無效----------------------------------")
38           // 只有在當前路由不是登錄頁面才跳轉
39           router.currentRoute.path !== 'login' &&
40             router.replace({
41               path: 'login',
42               query: { redirect: router.currentRoute.path },
43             })
44       }
45     }
46     //loadinginstace.close()
47     Message.error({
48         message:'加載失敗'
49     })
50     return Promise.reject(error)// 返回接口返回的錯誤信息
51 })
52 export default axios

在main.js中配置:

 1 import Vue from 'vue'
 2 import axios from './assets/js/interceptAxios'
 3 import VueAxios from 'vue-axios'
 4 import store from './store/store'
 5 import Qs from 'qs'
 6 
 7 
 8 Vue.prototype.HOST="/api"//解決跨域問題,做一個反向代理
 9 // 將axios掛載到prototype上,在組件中可以直接使用this.axios訪問
10 Vue.prototype.$http=axios
11 Vue.prototype.qs=Qs
12 Vue.prototype.store = store
13 
14 Vue.use(VueAxios,axios)

 

在vue中應用:

 1 <script>
 2 export default {
 3     data(){
 4      return {
5       msg:''
6     }  7 }, 8 methods:{ 9 tool(data){ 10 this.axios.post(this.HOST+'/vueHome/QueryTourOrigin.vue',this.qs.stringify(data)) 11 .then(function(res){ 12 console.log(res); 13 }) 14 } 15 } 16 } 17 </script>

以上只是些簡單的應用,應該還有更深層次的使用,待續......

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM