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);
    }
  });
}
相关推荐
一颗松鼠几秒前
JavaScript 闭包是什么?简单到看完就理解!
开发语言·前端·javascript·ecmascript
小远yyds20 分钟前
前端Web用户 token 持久化
开发语言·前端·javascript·vue.js
阿伟来咯~1 小时前
记录学习react的一些内容
javascript·学习·react.js
吕彬-前端1 小时前
使用vite+react+ts+Ant Design开发后台管理项目(五)
前端·javascript·react.js
学前端的小朱1 小时前
Redux的简介及其在React中的应用
前端·javascript·react.js·redux·store
guai_guai_guai1 小时前
uniapp
前端·javascript·vue.js·uni-app
bysking2 小时前
【前端-组件】定义行分组的表格表单实现-bysking
前端·react.js
王哲晓3 小时前
第三十章 章节练习商品列表组件封装
前端·javascript·vue.js
fg_4113 小时前
无网络安装ionic和运行
前端·npm
理想不理想v3 小时前
‌Vue 3相比Vue 2的主要改进‌?
前端·javascript·vue.js·面试