当前位置: 首页 > news >正文

axios请求设置request umijopenai生产前端请求 ts状态全局 v-if v-else 与动态js变量

axios请求

安装

npm install axios

全局自定义请求

集中处理设置 集体通用请求

example

const instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  timeout: 1000,
  headers: {'X-Custom-Header': 'foobar'}
});

请求前

请求后

拦截器

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    return response;
  }, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return Promise.reject(error);
  });

前后端交互

@umijs/openai插件

可以由后端kf4j文档 自动生成前端代码

npm i --save-dev @umijs/openapi

在根目录添加配置文件

import { generateService } from '@umijs/openapi'

generateService({
  requestLibPath: "import request from '@/request'",
  schemaPath: 'http://localhost:8123/api/v2/api-docs',
  serversPath: './src',
})

pack.json设置

//  what;
//  自己添加openapi服务 设置要执行哪些代码
//    使用 node 运行openapi.config.js js文件
    "openapi": "node openapi.config.js",

bug1 后端未启动

fetch openapi error: FetchError: request to http://localhost:8123/api/v2/api-docs failed, reason: 
    at ClientRequest.<anonymous> (D:\project\pioneak-picture-frontend\node_modules\node-fetch\lib\index.js:1501:11)

bug2 axios基础网址设置错误

const myAxios = axios.create({
  baseURL: 'http://localhost:8123',
  timeout: 60000,
  withCredentials: true,
});

全局状态管理

某一个信息多个页面共享数据

Pinia更新与共享

import { defineStore } from "pinia";
import { ref } from "vue";

export const useLoginUserStore = defineStore("loginUser", () => {
  const loginUser = ref<any>({
    userName: "未登录",
  });

  async function fetchLoginUser() {
    // todo 由于后端还没提供接口,暂时注释
    // const res = await getCurrentUser();
    // if (res.data.code === 0 && res.data.data) {
    //   loginUser.value = res.data.data;
    // }


    /*
    what:
      测试用户登录
      loginUser常量 设置对象内容 3s后设置
      app.vue 中 使用 LoginUserStore.fetchLoginUser() 设置loginUser value 在vue动态响应 <div v-if="loginUserStore.loginUser.id">判断到了id
     */
    setTimeout(() =>{
      loginUser.value = {userName: "测试用户", id: 1}
    },3000)
  }

  function setLoginUser(newLoginUser: any) {
    loginUser.value = newLoginUser;
  }

  return { loginUser, setLoginUser, fetchLoginUser };
});

使用状态全局

const LoginUserStore = useLoginUserStore()
LoginUserStore.fetchLoginUser()

在组件中使用状态全局对象与对象信息

<div v-if="loginUserStore.loginUser.id">

            {{loginUserStore.loginUser.userName ?? '无名'}}
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
const loginUserStore = useLoginUserStore()

相关文章:

  • C#中的字典怎么使用?
  • Linux10-共享队列
  • android智能指针android::sp使用介绍
  • 工程化与框架系列(4)--Webpack 高级配置详解
  • 从ETL到数仓分层:大数据处理的“金字塔”构建之道
  • 【MATLAB例程】三维下的IMM(交互式多模型),模型使用CV(匀速)和CA(匀加速)
  • Vue程序下载
  • 大白话TypeScript第七章TypeScript 与不同技术栈的深度融合及拓展应用
  • 【Cadence仿真学习笔记】ADS Dynamic Link报错model is reserved的解决办法
  • vue3在使用ts为模板引用标注类型时,vue3.5+版本有了全新写法
  • conda怎么迁移之前下载的环境包,把python从3.9升级到3.10
  • 非关系型数据库和关系型数据库的区别
  • 2025年度福建省职业院校技能大赛高职组“信息安全管理与评估”赛项规程样题模块二
  • Nginx系列06(Nginx 缓存配置、SSL/TLS 配置)
  • DeepSeek + 自由职业 发现新大陆,从 0 到 1 全流程跑通商业 IP
  • UE5 Slate类的基础创建
  • 【docker】docker swarm lock和unlock的区别,以及旧节点重启的隐患
  • JavaWeb基础专项复习5——请求对象和响应对象request and response
  • 【江科大STM32】TIM输出比较-PWM功能(学习笔记)
  • 小识ThreadLocal 用法
  • 网络优化工程师是做什么的/长沙关键词优化新报价
  • 沈阳网站外包公司/十种营销方式
  • 怎样销售网站/seo关键词外包
  • 钦州建设网站/百度竞价ocpc
  • 大良营销网站建设市场/变现流量推广app
  • 手机网络营销策划书/沈阳seo收费