添加了拦截器

This commit is contained in:
luyuan 2020-09-24 15:33:12 +08:00
parent 4fbf1353da
commit ed42b204d7
Signed by: theluyuan
GPG Key ID: A7972FD973317FF3
8 changed files with 42 additions and 13 deletions

View File

@ -12,7 +12,6 @@
"axios": "^0.20.0", "axios": "^0.20.0",
"core-js": "^3.6.5", "core-js": "^3.6.5",
"vue": "^3.0.0-0", "vue": "^3.0.0-0",
"vue-axios": "^2.1.5",
"vue-router": "^4.0.0-0", "vue-router": "^4.0.0-0",
"vuex": "^4.0.0-0" "vuex": "^4.0.0-0"
}, },

21
src/api/base.ts Normal file
View File

@ -0,0 +1,21 @@
import axios from '../config/axiosConfig'
import { AxiosPromise } from 'axios';
axios.interceptors.response.use((response)=>{
return response.data;
},(error)=>{
return error;
})
function get(url: string,data?: object): Promise<AxiosPromise> {
return axios.get(url,{params:data})
}
function post(url: string,data?: object): Promise<AxiosPromise> {
return axios.post(url,data)
}
export {
get,
post
}

10
src/api/index.ts Normal file
View File

@ -0,0 +1,10 @@
import { get, post } from './base'
import { AxiosPromise } from 'axios'
export function getinfo(): Promise<AxiosPromise>{
return get("a")
}
export function seninfo(data: object | undefined): Promise<AxiosPromise>{
return post("b",data)
}

View File

@ -1,8 +1,6 @@
import axios from 'axios' import axios from 'axios'
axios.defaults.baseURL = 'https://api.example.com'; axios.defaults.baseURL = 'https://theluyuan.com/api/';
axios.defaults.headers.common['Authorization'] = 'token'; axios.defaults.headers.common['Authorization'] = 'token';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
export default axios export default axios

View File

@ -4,8 +4,6 @@ import Antd from 'ant-design-vue';
import router from './router' import router from './router'
import store from './store' import store from './store'
import 'ant-design-vue/dist/antd.css'; import 'ant-design-vue/dist/antd.css';
import axios from "./config/axiosConfig"
import VueAxios from "vue-axios"
createApp(App).use(store).use(router).use(Antd).use(VueAxios,axios).mount('#app') const app = createApp(App);
app.use(store).use(router).use(Antd).mount('#app')

View File

@ -8,11 +8,18 @@
<script lang="ts"> <script lang="ts">
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src
import { getinfo } from "../api/index"
export default defineComponent({ export default defineComponent({
name: 'Home', name: 'Home',
components: { components: {
HelloWorld, HelloWorld,
}, },
setup(){
// ctx.axios.get("https://www.baidu.com")
getinfo().then((res)=>{
console.log(res)
})
}
}); });
</script> </script>

View File

@ -11,6 +11,7 @@
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"sourceMap": true, "sourceMap": true,
"baseUrl": ".", "baseUrl": ".",
"noImplicitThis": true,
"types": [ "types": [
"webpack-env" "webpack-env"
], ],

View File

@ -8726,11 +8726,6 @@ vm-browserify@^1.0.1:
resolved "https://registry.npmjs.org/vm-browserify/-/vm-browserify-1.1.2.tgz#78641c488b8e6ca91a75f511e7a3b32a86e5dda0" resolved "https://registry.npmjs.org/vm-browserify/-/vm-browserify-1.1.2.tgz#78641c488b8e6ca91a75f511e7a3b32a86e5dda0"
integrity sha512-2ham8XPWTONajOR0ohOKOHXkm3+gaBmGut3SRuu75xLd/RRaY6vqgh8NBYYk7+RW3u5AtzPQZG8F10LHkl0lAQ== integrity sha512-2ham8XPWTONajOR0ohOKOHXkm3+gaBmGut3SRuu75xLd/RRaY6vqgh8NBYYk7+RW3u5AtzPQZG8F10LHkl0lAQ==
vue-axios@^2.1.5:
version "2.1.5"
resolved "https://registry.npm.taobao.org/vue-axios/download/vue-axios-2.1.5.tgz#1af4bf1218ed71309c76afb38d0f683e312c24a7"
integrity sha1-GvS/EhjtcTCcdq+zjQ9oPjEsJKc=
vue-eslint-parser@^7.0.0, vue-eslint-parser@^7.1.0: vue-eslint-parser@^7.0.0, vue-eslint-parser@^7.1.0:
version "7.1.0" version "7.1.0"
resolved "https://registry.npmjs.org/vue-eslint-parser/-/vue-eslint-parser-7.1.0.tgz#9cdbcc823e656b087507a1911732b867ac101e83" resolved "https://registry.npmjs.org/vue-eslint-parser/-/vue-eslint-parser-7.1.0.tgz#9cdbcc823e656b087507a1911732b867ac101e83"