当前位置: 首页 > 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()
http://www.lryc.cn/news/546063.html

相关文章:

  • SparkSQL全之RDD、DF、DS ,UDF、架构、资源划分、sql执行计划、调优......
  • 深入理解Linux内存缓存:提升性能的关键
  • STM32-FOC-SDK包含以下关键知识点
  • sql调优:优化响应时间(优化sql) ; 优化吞吐量
  • 【Mybatis】如何简单使用mybatis-plus,以及MybatisGenerator自动生成或者实现SQL语句
  • Halcon 车牌识别-超精细教程
  • LeetCode 25 - K 个一组翻转链表
  • 一文读懂智能硬件定位:开启智能时代的精准导航
  • 夸父工具箱(安卓版) 手机超强工具箱
  • Html5学习教程,从入门到精通,HTML5 列表语法知识点及案例代码(11)
  • 内核进程调度队列(linux的真实调度算法) ─── linux第13课
  • 16.7 LangChain LCEL 极简入门:Prompt + LLM 的黄金组合
  • Spring线程池学习笔记
  • ArcGIS操作:08 计算shp面积并添加到属性表
  • 安卓音频框架混音器
  • 左值引用与指针的区别
  • Linux基础使用和程序部署
  • Linux驱动开发之串口驱动移植
  • 计算机毕业设计SpringBoot+Vue.js美食推荐系统商城(源码+文档+PPT+讲解)
  • 指针小节.
  • [Qt5] QJson数据之间的转换以及QByteArray图像数据压缩
  • 2025年能源工作指导意见
  • Android 获取jks的SHA1值:java.io.IOException: Invalid keystore format
  • 深入探索像ChatGPT这样的大语言模型-02-POST training supervised finetuning
  • 广义线性模型下的数据分析(R语言)
  • AutoMQ:无需 Cruise Control 实现 Kafka 的自动分区再平衡
  • 在剪映中给英文学习视频添加中文字幕
  • Opencv之sift特征检测和FLANN 匹配器进行指纹特征匹配
  • rust学习~tokio的io
  • FPGA开发,使用Deepseek V3还是R1(2):V3和R1的区别