You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Landy React TypeScript模板添加带ProtectedRoute的新路由方法咨询

Landy React TypeScript模板添加带ProtectedRoute的新路由方法咨询

我来帮你梳理下怎么在这个Landy的TypeScript模板里实现带ProtectedRoute的新路由。这个模板用了路由数组统一管理的方式,和你习惯的直接写Route标签的思路略有不同,但调整起来很简单,分两步走就行:

第一步:在路由配置数组里添加新路由项

首先你得找到项目里的路由配置数组(一般叫routes,可能在src/routes/index.ts或者类似的文件里)。给这个数组加上你需要的新路由,并且新增一个protected标记来区分是否需要路由保护:

// 示例路由数组,根据你项目的实际结构调整
const routes = [
  // 这里是项目原有的路由项,比如首页、登录页等
  { path: '/', exact: true, component: 'HomePage' },
  { path: '/login', exact: true, component: 'LoginPage' },
  
  // 新增你的受保护路由
  {
    path: '/thanks',
    exact: true,
    component: 'ThankyouPage',
    protected: true // 标记这个路由需要ProtectedRoute包裹
  },
  {
    path: '/revised',
    exact: true,
    component: 'ThankyouEditPage',
    protected: true
  }
];

第二步:修改Router组件的路由渲染逻辑

原来的模板是直接把懒加载的组件传给Route的component属性,现在我们需要根据protected标记动态包裹ProtectedRoute,并且改用element属性(对应你习惯的React Router写法)。修改后的Router组件代码如下:

import { lazy, Suspense } from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import I18nextProvider from 'react-i18next/I18nextProvider';
import ProtectedRoute from './path/to/ProtectedRoute'; // 记得替换成你ProtectedRoute的实际路径
import i18n from './path/to/i18n'; // 项目原有的i18n配置
import Styles from './Styles';
import Header from './Header';
import Footer from './Footer';

const App = () => (
  <BrowserRouter>
    <I18nextProvider i18n={i18n}>
      <Router />
    </I18nextProvider>
  </BrowserRouter>
);

const Router = () => {
  return (
    <Suspense fallback={null}>
      <Styles />
      <Header />
      <Switch>
        {routes.map((routeItem) => {
          // 懒加载对应的页面组件
          const LazyPageComponent = lazy(() => import(`../pages/${routeItem.component}`));
          
          // 根据protected标记决定是否包裹ProtectedRoute
          let routeContent = <LazyPageComponent />;
          if (routeItem.protected) {
            routeContent = <ProtectedRoute>{routeContent}</ProtectedRoute>;
          }

          return (
            <Route
              key={routeItem.path} // 用路径当key比component更稳妥,避免组件名重复
              path={routeItem.path}
              exact={routeItem.exact}
              element={routeContent} // 改用element属性传递组件内容
            />
          );
        })}
      </Switch>
      <Footer />
    </Suspense>
  );
};

export default App;

额外说明

  • 如果你的项目用的是React Router v5(而不是v6),那element属性是不存在的,需要改用render属性来返回组件内容,代码调整成这样:
<Route
  key={routeItem.path}
  path={routeItem.path}
  exact={routeItem.exact}
  render={() => {
    const LazyPageComponent = lazy(() => import(`../pages/${routeItem.component}`));
    let content = <LazyPageComponent />;
    if (routeItem.protected) {
      content = <ProtectedRoute>{content}</ProtectedRoute>;
    }
    return content;
  }}
/>
  • 一定要确保ProtectedRoute组件已经正确导入到Router组件中,路径要和你项目里的实际文件位置对应。

这样调整后,既保留了模板原有的功能式路由管理风格,又实现了你需要的路由保护需求,和你习惯的React写法逻辑是一致的,只是把路由配置抽成了数组来统一管理~

备注:内容来源于stack exchange,提问作者Kris Swat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 11:29:37