
前のプロジェクトでは、リクエスト API とリクエスト メソッドがカプセル化されています。このカプセル化は、簡素化、バックエンドによって提供されるインターフェイスの管理の向上、リクエスト コードの再利用性、および簡単なコード変更。
axios のインストール
ディレクトリ ファイルの作成
src に http ディレクトリを作成
http Createディレクトリに http.js ユーザーを作成し、メソッドをリクエストします。
http ディレクトリに api.js を作成して、バックエンドが提供するインターフェイスを保存します。
http ディレクトリに axios.js ユーザーを作成して、 axios インターセプトを実行します。デバイス
ルート ディレクトリに vue.config.js を作成します。ユーザーはプロキシ設定を要求します。
次のコードです。
project/scr/http/http のコードです。 .js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import axios from 'axios';
export default {
get(url, auth = false) {
if (auth) {
return axios.get(url, {headers: {Authorization: 'Your back- end user authenticates information'}});
} else {
return axios.get(url);
}
},
post(url, data, auth = false) {
if (auth) {
return axios.post(url, data, {headers: {Authorization: 'Your back- end user authenticates information'}});
} else {
return axios.post(url, data);
}
},
put(url, data, auth = false) {
if (auth) {
return axios.put(url, data, {headers: {Authorization: 'Your back- end user authenticates information'}});
} else {
return axios.put(url, data);
}
},
del(url, auth = false) {
if (auth) {
return axios. delete (url, {headers: {Authorization: 'Your back- end user authenticates information'}});
} else {
return axios. delete (url);
}
},
uploader(url, file, auth = false) {
let param = new FormData();
param.append('file', file)
if (auth) {
return axios.post(url, param, {headers: {Authorization: 'Your back- end user authenticates information'}})
} else {
return axios.post(url, param)
}
},
}
|
ログイン後にコピー
project/scr/http/ にファイル API を作成し、管理を容易にするためにプロジェクト モジュールに従ってインターフェイス ファイルを作成できます。
Project/scr/http/api。 jsコード
1 2 3 4 5 6 7 8 9 10 11 12 13 | import Goods from './api/goods.js';
export default {
Index: {
index: '/index/index'
},
Home: {
UserInfo: '/user/info'
},
Goods: Goods
}
|
ログイン後にコピー
project/scr/http/api/goods.jsのコード
1 2 3 | export default {
GoodsShow: '/goods/ default '
}
|
ログイン後にコピー
project/scr/http/axios.jsのコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import axios from 'axios';
axios.interceptors.request. use (config => {
return config
}, error => {
return Promise.reject(error)
})
axios.interceptors.response. use (response => {
return response;
}, error => {
if (error && error.response) {
switch (error.response.status) {
case 400:
break
case 401:
break
default :
return Promise.reject(error);
}
}
})
|
ログイン後にコピー
上記は準備ができて。次のステップでは、project/scr/min.js にコードを追加します。
project/scr/min.js のコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import Vue from 'vue';
import App from './App.vue';
import api from './http/api';
import http from './http/http';
import './http/axios'
Vue.prototype. $api = api;
Vue.prototype. $http = http;
那么接下来就是使用了
项目 /src/views/index/index.vue 中我们对他进行使用
<template>
<div>
</div>
</template>
<script>
export default {
name: "Index" ,
mounted() {
this.handle()
},
methods: {
handle(){
this. $http .get(this. $api .Index.index,true).then((result) => {
})
}
}
}
</script>
<style scoped>
</style>
|
ログイン後にコピー
次のステップは、プロキシ構成を追加することです
project/vue .config.js コード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | const target = process.env.APP_API_URL;
module.exports = {
devServer: {
proxy: {
'/api': {
target: target,
changeOrigin: true,
ws: true,
pathRewrite: {
'^api': ''
}
}
}
}
}
|
ログイン後にコピー
推奨チュートリアル: "PHP チュートリアル " "JS チュートリアル "
以上がVue での Axios のカプセル化の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。