vue3.0之axios的请求的封装

vue3.0之axios的请求的封装

  • 安装:
    • npm i axios --save
  • 使用:
    • utils/api/http.ts
import axios from "axios";

const request = axios.create({
  // baseURL: 'http://120.76.247.5:2001/api',
  baseURL: process.env.VUE_APP_BASEURL, // 根据不同的环境 区分不同的基础url
  withCredentials: true,
});

export default request;
  • 区分不同的环境
  • 在项目的根目录下创建以下两个文件 .env.development.env.production
  • 1:.env.development
VUE_APP_BASEURL = http://huangjinggjun.top:9001/movie/
  • 2:.env.production
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";
// , getComingSoon, getTop250s
import { getInTheaters } from "../utils/api/movie";
export default defineComponent({
  name: "Home",
  components: {},
  setup() {
    const state = reactive({
      inTheaters: [], // 正在热映
      comingSoon: [], // 即将上映
      top250: [] // 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>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值