vue3.0之axios的请求的封装
import axios from "axios";
const request = axios.create({
baseURL: process.env.VUE_APP_BASEURL,
withCredentials: true,
});
export default request;
- 在项目的根目录下创建以下两个文件
.env.development
与 .env.production
- 1:
.env.development
VUE_APP_BASEURL = http://huangjinggjun.top:9001/movie/
VUE_APP_BASEURL = http://XXX.top:9001/movie/
实际的请求 api/movei.ts
import http from "./http";
export const getInTheaters = ({ start = 0, count = 5 }) => {
return http({
url: `in_theaters?start=${start}&count=${count}`,
});
};
使用请求
<template>
<div>我是home</div>
</template>
<script lang='ts'>
import { defineComponent, reactive, toRefs, onMounted } from "vue";
import { getInTheaters } from "../utils/api/movie";
export default defineComponent({
name: "Home",
components: {},
setup() {
const state = reactive({
inTheaters: [],
comingSoon: [],
top250: []
});
const getInTheatersData = () => {
let obj = { start: 0, count: 5 };
getInTheaters(obj).then(res => {
console.log("res", res);
});
};
onMounted(() => {
getInTheatersData();
});
return {
...toRefs(state)
};
}
});
</script>
<style lang="scss" scoped>
</style>