React18+React-router-domV6+and5前端脚手架

一、脚手架目录

二、路由配置

1、JSON路由树示例:

js 复制代码
import {lazy} from 'react'
import BasicLayout from '@layout/index.jsx';

export const store_routes = [
    {
        path: '/personal',
        isLazy:true,
        component:lazy(() => import('@pages/personal/index.jsx')),
    },
    {
        path: '/roleConfig',
        isLazy:true,
        component: lazy(() => import('@pages/roleConfig/index.jsx')),
    },
    {
        path: '/routeConfig',
        isLazy:true,
        component: lazy(() => import('@pages/routerConfig/index.jsx')),
    },
    { from: '*',to: '/404'}
]

const getRouter = (routes) => {
    const router = [
        {
            path: '/login',
            isLazy:true,
            component: lazy(() => import('@pages/HomePage/index.jsx'))
        },
        {
            path: '/404',
            isLazy:true,
            component: lazy(() => import('@component/NoFound/index.jsx'))
        },
        {
            path: '/',
            isLazy:false,
            component: BasicLayout,
            children: [
                ...store_routes
            ]
        },
    ]
    return router;
}
export default getRouter;

2、使用React-router-domV6针对JSON路由树进行路由配置

js 复制代码
import React, { Suspense } from 'react'
import { HashRouter as Router, Route, Routes, Navigate } from 'react-router-dom'
import getRouter from './router';
import { Loading } from '@component/index'

const renderRouter = (routes) => {
  if (!routes) {
    return null
  }
  return (
    <>
      {
        routes.map((route, index) =>
          route.path ? (
            <Route
              key={index}
              path={route.path}
              element={
                route.isLazy ? (
                  <Suspense fallback={<Loading />}>
                    <route.component />
                  </Suspense>
                ) : (
                  <route.component />
                )
              }
            >
              {route.children && renderRouter(route.children)}
            </Route>
          ) : (
            <Route
              key={index}
              path={route.from}
              element={<Navigate to={route.to} replace />}
            />
          )
        )
      }
    </>
  )
}


const RouterConfig = ({ routerData }) => {
  return (
    <Router>
      <Routes>
        {renderRouter(getRouter(routerData))}
      </Routes>
    </Router>
  )
}
export default RouterConfig;

三、多语配置

1、创建对应的多语文件,例如:en.json、zh-cn.json和zh-hk.json,并文件中编写对应的多语,具体如下所示:

2、将多语文件引入并配置多语:

js 复制代码
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from 'i18next-browser-languagedetector';
import translation_en from "@locales/language/en.json";
import translation_zh from "@locales/language/zh-cn.json";
import translation_hk from "@locales/language/zh-hk.json";


const resources = {
  en: {
    translation: translation_en,
  },
  cn: {
    translation: translation_zh,
  },
  hk: {
    translation: translation_hk,
  },
};
i18n.use(LanguageDetector) //当前浏览器语言
i18n.use(initReactI18next).init({
  resources,
  lng: '',
  interpolation: {
    escapeValue: false,
  },
});

export default i18n;

四、路由集成和多语组装

1、在containers目录下创建app.jsx集成路由并组装多语,示例如下:

js 复制代码
import React,{useEffect} from 'react'
import { ConfigProvider } from 'antd';
import { connect, Provider } from 'react-redux';
import RouterConfig from '@router/routerConfig';
import enUS from 'antd/es/locale/en_US';
import zhCN from 'antd/es/locale/zh_CN';
import zhTW from 'antd/es/locale/zh_TW';

import 'antd/dist/antd.css'
import '@locales/index.js'
import {setMobileCookie,geMobiletCookie} from '@utils/cookieSet'
import * as actions from '@locales/store/action';

const App = (props) => {
    useEffect(() => {
        let lang = geMobiletCookie('isCns') != null ? geMobiletCookie('isCns') : props.currentLocale
        setMobileCookie("isCns",  lang, 1)
        var intDate = props.getCurrentLocaleRequest(lang)
        return () => {
            clearInterval(intDate);
        }
    }, [])
    const locale = props.currentLocale == 'en' ? enUS  : props.currentLocale == 'cn' ? zhCN : zhTW;
    return (
        <ConfigProvider locale={locale}>
            <Provider store={props.store}>
                <RouterConfig/>
            </Provider>
        </ConfigProvider>
    );

}
const mapStateToProps = (state) => ({
    currentLocale: state.currentLocaleConfig.currentLocale,
})
const mapDispatchToProps = dispatch => ({
    // 获取当前语言
    getCurrentLocaleRequest(values) {
        dispatch(actions.getCurrentLocaleRequest(values))
    }

})
export default connect(mapStateToProps, mapDispatchToProps)(App)

2、初始渲染:将元素渲染到root,示例如下:

js 复制代码
import React from 'react';
import { createRoot } from 'react-dom/client';
import App from '@containers/app.jsx'
import store from './store.js'

const container = document.getElementById('root');
const root = createRoot(container);
root.render(<App store={store} />);
// 热更新
if (module.hot) {
  module.hot.accept(err => {
    if (err) {
      console.error('module.hot,', err);
    }
  });
}
相关推荐
Moment2 分钟前
长上下文会最终杀死 Rag 吗?
前端·javascript·后端
qcx2330 分钟前
【系统学AI】25 论文导读 ①:两篇改变 AI 的开山之作——Attention Is All You Need & ReAct
前端·人工智能·react.js·transformer
kyriewen1 小时前
大文件上传最全指南:分片、断点续传、秒传,一篇就够了
前端·javascript·面试
郑洁文2 小时前
基于Python的Web命令执行漏洞自动化检测系统
前端·python·网络安全·自动化
新酱爱学习2 小时前
手搓 10 个 Skill 后,我把重复劳动收敛成了一套零依赖 CLI 工具
前端·javascript·人工智能
IT_陈寒3 小时前
Python的线程池居然把我坑在了垃圾回收这块
前端·人工智能·后端
研☆香3 小时前
es6新特性功能介绍(一)
前端·ecmascript·es6
陈_杨3 小时前
鸿蒙开发-疾阅App阅读训练功能技术解析
前端·javascript
zhangxingchao3 小时前
AI应用开发八:RAG相关技术总结
前端·人工智能·后端