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

react/umi,浏览器tab设置

场景:umi3升级到umi4以后,浏览器tab本来是跟随路由变化而变化,升级后浏览器tab失效

 1. @ant-design/pro-layout 默认内置了为 document.title 同步路由 name 的机制。

     它里面在 layout/breadcrumb 渲染和路由切换时会自动执行 document.title = currentPage.name || ...。 即使你不配 title,不写 useEffect,也能自动切。

 2. 升级4后换掉了引用的 pro-layout ,(无论是 ali定制、x-ops还是其他基于 ProLayout 的库)可能删掉了这个内置逻辑。

     通常公司定制的 pro-layout,不会主动帮你同步 document.title,或者 title 的来源不是路由 name,而是单独一份配置、或你必须在页面级做 set。

 3. umi4 的 router 和 layout 声明式路由变化很大,原来的路由 name/title 不能自动同步到 tab。

 4. 新版不主动设置 document.title,页面不变就是因为没人调用了那个 set。

解决: 路由配置只有name没有title,不变 。   

            layouts文件,添加根据 route 文件,当前路由切换修改浏览器tab

import { history, Outlet, useLocation } from 'umi';
import routes from '../../config/routes';const location = useLocation();// 根据路径查找路由配置
const findRouteByPath = (routes: any[], path: string) => {// 标准化路径,去除末尾的斜杠(除非是根路径)const normalizedPath = path.length > 1 ? path.replace(/\/+$/, '') : path;for (const route of routes) {// 标准化路由路径const normalizedRoutePath = route.path.length > 1 ? route.path.replace(/\/+$/, '') : route.path;// 完全匹配if (normalizedRoutePath === normalizedPath) {return route;}// 前缀匹配检查是否为子路由if (route.routes && normalizedPath.startsWith(normalizedRoutePath + '/')) {const found = findRouteByPath(route.routes, normalizedPath);if (found) {return found;}}// 递归搜索子路由if (route.routes) {const found = findRouteByPath(route.routes, normalizedPath);if (found) {return found;}}}return null;
};// 监听路由变化并更新页面标题useEffect(() => {const updateDocumentTitle = () => {const currentRoute = findRouteByPath(routes, location.pathname);if (currentRoute && currentRoute.name) {document.title = `${currentRoute.name} - ${settings.title}`;} else {document.title = settings.title;}};// 立即更新一次标题updateDocumentTitle();// 监听路由变化 重置错误状态并更新标题const unlisten = history.listen(() => {errorBoundaryRef.current?.resetError();updateDocumentTitle();});return () => {unlisten();};}, [location.pathname, routes]);

http://www.dtcms.com/a/390596.html

相关文章:

  • langchain-PipelinePromptTemplate
  • git 本地仓库与远程仓库链接
  • 绘想 - 百度推出的AI视频创作平台
  • 穿越像素的凝视:深度解析视频中的人物与动物识别算法技术
  • OpenHarmony 4.0 Release源码下载、编译及烧录
  • 大模型提示词Prompt工程:2-全攻略+最佳实践框架+原理解析+实战案例库+七招要诀
  • 大模型微调——Prompt-Tuning
  • code2prompt 快速生成项目 Markdown 文档(结合大模型进行问答)
  • UIKit-CAGradientLayer
  • K8s LoadBalancer服务深度解析
  • Windows 系统开发 iOS 与安卓应用全流程指南,附 PC 前端工具链
  • CentOS 7 系统 “cannot find a valid baseurl for repo base7x86_64” 报错完整解决方案
  • centos7通过kubeadm安装k8s1.27.1版本
  • kubesphere(k8s)如何设置存储类的默认路径
  • 在 k8s 上部署 Kafka 4.0 3节点集群
  • k8s 部署 EMQX 5.8.6 静态三节点集群
  • UVa1374/LA3621 Power Calculus
  • 以 NoETL 重塑 AI-Ready 的数据底座,Aloudata 获评 IDC 面向生成式 AI 的数据基础设施核心厂商
  • 声音转文字API平台推荐
  • Vue3: watch watchEffect
  • 梯度提升算法及其在回归与分类中的应用实战
  • 【自然语言处理与大模型】大模型应用开发四个场景
  • 深度神经网络-传播原理
  • 交通仿真术语
  • 关于Oracle主外键约束的几个SQL语句
  • Python 操作 SQLite:Peewee ORM 与传统 sqlite3.connect 的全方位对比
  • go资深之路笔记(四)中间件(Middleware)设计模式
  • MySQL分库分表迁移:ETL平台如何实现数据合并与聚合
  • [极客大挑战 2019]BabySQL
  • SQL-索引使用