添加了拦截器
This commit is contained in:
21
src/api/base.ts
Normal file
21
src/api/base.ts
Normal 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
10
src/api/index.ts
Normal 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)
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
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.post['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
|
||||
|
||||
|
||||
export default axios
|
||||
@@ -4,8 +4,6 @@ import Antd from 'ant-design-vue';
|
||||
import router from './router'
|
||||
import store from './store'
|
||||
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')
|
||||
|
||||
@@ -8,11 +8,18 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue';
|
||||
import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src
|
||||
import { getinfo } from "../api/index"
|
||||
|
||||
export default defineComponent({
|
||||
name: 'Home',
|
||||
components: {
|
||||
HelloWorld,
|
||||
},
|
||||
setup(){
|
||||
// ctx.axios.get("https://www.baidu.com")
|
||||
getinfo().then((res)=>{
|
||||
console.log(res)
|
||||
})
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user