• vue3 axios


     npm install axios
    1. import axios from 'axios'
    2. // 创建axios实例
    3. const request = axios.create({
    4. baseURL: '',// 所有的请求地址前缀部分(没有后端请求不用写)
    5. timeout: 80000, // 请求超时时间(毫秒)
    6. withCredentials: true,// 异步请求携带cookie
    7. // headers: {
    8. // 设置后端需要的传参类型
    9. // 'Content-Type': 'application/json',
    10. // 'token': x-auth-token',//一开始就要token
    11. // 'X-Requested-With': 'XMLHttpRequest',
    12. // },
    13. })
    14. // request拦截器
    15. request.interceptors.request.use(
    16. config => {
    17. // 如果你要去localStor获取token,(如果你有)
    18. // let token = localStorage.getItem("x-auth-token");
    19. // if (token) {
    20. //添加请求头
    21. //config.headers["Authorization"]="Bearer "+ token
    22. // }
    23. return config
    24. },
    25. error => {
    26. // 对请求错误做些什么
    27. Promise.reject(error)
    28. }
    29. )
    30. // response 拦截器
    31. request.interceptors.response.use(
    32. response => {
    33. // 对响应数据做点什么
    34. return response.data
    35. },
    36. error => {
    37. // 对响应错误做点什么
    38. return Promise.reject(error)
    39. }
    40. )
    41. export default request

    1. import instance from "./request";
    2. // post请求,有参数,如传用户名和密码
    3. export const loginAPI = (data: any)=>{
    4. return instance.post("/admin/login", data);
    5. }
    1. const { defineConfig } = require("@vue/cli-service");
    2. module.exports = defineConfig({
    3. transpileDependencies: true,
    4. lintOnSave: false,
    5. plugins: [
    6. vue(),
    7. //...
    8. ],
    9. server: {
    10. proxy: {
    11. '/api': { // 匹配请求路径,
    12. target: '你要代理的地址', // 代理的目标地址
    13. // 开发模式,默认的127.0.0.1,开启后代理服务会把origin修改为目标地址
    14. changeOrigin: true,
    15. // secure: true, // 是否https接口
    16. // ws: true, // 是否代理websockets
    17. // 路径重写,**** 如果你的后端有统一前缀(如:/api),就不开启;没有就开启
    18. //简单来说,就是是否改路径 加某些东西
    19. rewrite: (path) => path.replace(/^\/api/, '')
    20. }
    21. }
    22. }
    23. });
    1. import { loginAPI } from "../../utils/api";
    2. //直接使用,一般用在进入页面入请求数据的接口
    3. let user = ref({
    4. uer: 1234,
    5. password: 12344
    6. })
    7. const loginAPI = async () => {
    8. let res = await loginAPI({
    9. ...user,
    10. })
    11. console.log("***", res);
    12. let { list, pageNum, pageSize, total } = res.data
    13. console.log(list, pageNum, pageSize, total);
    14. }

  • 相关阅读:
    URL endoce 和 decode
    Dubbo管理控制台dubbo-admin搭建
    迁移 MySQL 数据到 OceanBase 集群
    领先实践|全球最大红酒App如何用设计冲刺创新vivino模式
    今日多写一行注释,明日维护少掉一根头发
    认识时间复杂度和简单排序算法
    代码随想录day39 || 动态规划 || 不同路径
    【无标题】
    Android开发学习——3.平台版本、SDK版本、API级别
    Css框架之星星评价功能
  • 原文地址:https://blog.csdn.net/YUlangML/article/details/133643350