dva去掉访问路径中的“#”

本文介绍如何在项目中配置BrowserHistory替代hashRouter,通过具体步骤实现更好的SEO效果,并提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1) 终端中安装: npm install --save history
2) 在入口的src/ index.js中 导入
import {createBrowserHistory as createHistory} from “history”
3) 修改dva初始化内容 src/index.js中
const app =dva({history:createHistory()});

如上就把#去掉了,这样通过 ‘localhost:8000/ ’ 就可以访问根页面了。
默认是hashRouter 对浏览器不太好,通过上面的方式转换为BrowserHistory。需要用seo来抓取这个页面
历史变化: # 锚点 最早通过#来实现单页面,跳转到不同位置; history最早是H5的history记录网页地址,后退前进一步 back(),forward()

如果地址中间要加一个#那么
把引入的BrowserRouter 改为HashRouter

patchClientRoutes更新完路由之后,如何触发侧边栏路由显示的更新呢?目前侧边栏的显示实现如下,如何调整:import { ReactNode, useEffect, useMemo } from "react"; import { history } from "@/utils/umi"; import classNames from "classnames"; import { Menu } from "@sigma-wg/ui"; import { useMenuSelectedKey } from "@/hooks/use-menu-selected-key"; import { useSiteShortName } from "@/store/host-url"; import { useAdminRoutes } from "@/store/admin-routes"; import { getItemIcon } from "./helper"; import { useCurrentRoute, getCurrentApp } from "../hooks"; import styles from "./index.module.css"; interface SiderProps { collapsed: boolean; } const LabelByCollapsed = ({ children, collapsed, }: { children: ReactNode; collapsed: boolean; }) => { return !collapsed ? children : <></>; }; const Sider = ({ collapsed }: SiderProps) => { const selectedKey = useMenuSelectedKey(); const appName = getCurrentApp(selectedKey); const currentRoute = useCurrentRoute(appName); const childAdminRoute = useAdminRoutes(); // 去除重定向的路由 const currentRoutes = useMemo(() => { console.log("childAdminRoute",childAdminRoute); const parentRoutes = currentRoute?.children?.filter((item) => item.path !== "/" && !item.path?.includes(&#39;*&#39;)) || []; const parentRoutePaths = parentRoutes.map((route) => route.path); const childAdminRoutes = childAdminRoute?.filter((item) => parentRoutePaths.includes(item.path)); return [...parentRoutes,...childAdminRoutes] }, [currentRoute,childAdminRoute]); const site_short_name = useSiteShortName(); const items = useMemo(() => { console.log("currentRoutes",currentRoutes); const labelTitle = ( <div className={classNames(styles.label, styles.top)}> <LabelByCollapsed collapsed={collapsed}> <div className={styles.title}>{site_short_name || "智算"}</div> </LabelByCollapsed> </div> ); const subMenus = currentRoutes.map((g, index) => ({ label: ( <LabelByCollapsed collapsed={collapsed}>{g.label}</LabelByCollapsed> ), key: g.path || `${index}_${g.label}`, icon: getItemIcon(g.path), type: g.children && "group", // 有子菜单的 才有分组 children: g.children // @ts-ignore ?.filter((child) => !child.hidden) .map((m) => ({ key: m.path as string, label: m.label, icon: getItemIcon(m.path), })), })); return [ { type: "group", label: labelTitle, key: "title", children: subMenus, }, ]; }, [collapsed, site_short_name,currentRoutes]); // 找到当前路由页面有tab页的 eg: [{path:"/virtual-machine",routes:[{path:"/virtual-machine/virtual-machine-instance",...},{path:"/virtual-machine/virtual-machine-snapshot",...}]}] const parentTabRoutes = useMemo(() => { const hasTabRoutes: { path?: string; routes: Record<string, any>[] }[] = []; currentRoutes.forEach((parent) => { parent?.children?.forEach((child) => { // @ts-ignore if (child?.routes?.find((grandchild) => !!grandchild.hidden)) { hasTabRoutes.push({ path: child.path, // @ts-ignore routes: child.routes?.filter((grandchild) => !!grandchild.hidden), }); } }); }); return hasTabRoutes || []; }, [currentRoutes]); const handleMenuItemClick = (e: { key: string }) => { const parentRoutesPaths = parentTabRoutes.map((item) => item.path); // 1)路由对应页面有tab页的,则跳转到第一个tab页的地址 if (parentRoutesPaths.includes(e?.key)) { const findParentRoute = parentTabRoutes.find( (item) => item.path === e?.key, ); const firstTabPath = findParentRoute?.routes?.[0]?.path; if (firstTabPath) { history.push(firstTabPath); } } else { // 2)路由对应页面没有tab页的,直接路由跳转 history.push(e?.key); } }; return ( <Menu items={items} selectedKeys={[selectedKey]} onClick={handleMenuItemClick} className={styles.menu} /> ); }; export default Sider;
最新发布
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨寂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值