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

news/2025/3/1 15:59:40/

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.ppmy.cn/news/1575811.html

相关文章

南金研CAN数据记录仪如何成为新能源汽车售后的“好帮手”

南金研CAN数据记录仪在新能源汽车售后领域是一个非常实用的工具&#xff0c;能够有效解决售后过程中的诸多痛点问题。 偶发性故障诊断 新能源汽车的电子系统复杂&#xff0c;很多故障是偶发性的&#xff0c;难以在常规检测中复现。南金研CAN数据记录仪可以长时间部署在车辆上&…

【MySQL】索引(中)

欢迎拜访&#xff1a;雾里看山-CSDN博客 本篇主题&#xff1a;【MySQL】索引(中) 发布时间&#xff1a;2025.2.28 隶属专栏&#xff1a;MySQL 目录 一个现象现象展现现象解释为何IO交互要是 Page 构建B索引理解单个page理解多个Page页目录单页目录多页情况 复盘一下 为什么是B树…

关于Postman自动获取token

在使用postman测试联调接口时&#xff0c;可能每个接口都需要使用此接口生成的令牌做Authorization的Bearer Token验证&#xff0c;最直接的办法可能会是一步一步的点击&#xff0c;如下图&#xff1a; 在Authorization中去选择Bearer Token&#xff0c;然后将获取到的token粘贴…

SQL 中的 EXISTS 子句:探究其用途与应用

目录 EXISTS 子句简介 语法 EXISTS 与 NOT EXISTSEXISTS 子句的工作原理实际应用场景 场景一&#xff1a;筛选存在关联数据的记录场景二&#xff1a;优化查询性能 EXISTS 与其他 SQL 结构的比较 EXISTS vs. JOINEXISTS vs. IN 多重 EXISTS 条件在 UPDATE 语句中使用 EXISTS常见…

Spring Boot + Redis + Sa-Token

参考文献 Sa-Token实现分布式登录鉴权&#xff08;Redis集成 前后端分离&#xff09;-腾讯云开发者社区-腾讯云 介绍 StpInterface 是 Sa-Token 框架中的一个接口&#xff0c;属于 Sa-Token 身份认证与授权框架的一部分。该接口提供了一些方法来实现自定义的身份认证和授权管…

Web自动化之Selenium添加网站Cookies实现免登录

在使用Selenium进行Web自动化时&#xff0c;添加网站Cookies是实现免登录的一种高效方法。通过模拟浏览器行为&#xff0c;我们可以将已登录状态的Cookies存储起来&#xff0c;并在下次自动化测试或爬虫任务中直接加载这些Cookies&#xff0c;从而跳过登录步骤。 Cookies简介 …

信息系统的安全防护

文章目录 引言**1. 物理安全****2. 网络安全****3. 数据安全****4. 身份认证与访问控制****5. 应用安全****6. 日志与监控****7. 人员与管理制度****8. 其他安全措施****9. 安全防护框架**引言 从技术、管理和人员三个方面综合考虑,构建多层次、多维度的安全防护体系。 信息…

搭建elasticsearch集群,8.17.0版本

搭建集群的帖子有很多&#xff0c;但是似乎新版做了一些改动。 下面说一下我启动的步骤&#xff1a;先用如下命令启动第一个节点 docker run --name es01 -it --privileged --memory"2g" \-p 9200:9200 -p 9300:9300 \-e cluster.namemy-application \-e node.name…