全局配置axios:
示例代码:
//main.js中的配置
import axios from "axios";
const app=createApp(App)
axios.defaults.baseURL="https://www.escook.cn"//此处设置请求的根路径
//此处配置全局变量,后期使用this.$http调用,此处$http可自定义
app.config.globalProperties.$http=axios
app.mount('#app')
//get请求,post请求类似
<template>
<div class="box">
<h3>send get request</h3>
<button @click="sendGet">get request</button>
</div>
</template>
<script>
export default {
name: "get",
methods: {
async sendGet() {
const {data: ret} = await this.$http({
method: "GET",
url: '/api/get',
params: {
name: 'ls',
age: 33,
}
})
console.log(ret)
}
}
}
</script>
//post请求
<template>
<div class="box">
<h3>send post request</h3>
<button @click="sendPost">get request</button>
</div>
</template>
<script>
export default {
name: "post",
methods:{
async sendPost(){
const {data:ret}=await this.$http.post('/api/post',{name:'zs',age:20})
console.log(ret)
}
}
}
</script>