Эх сурвалжийг харах

新增router文件,暂时不使用

JXDS18FUJT 2 жил өмнө
parent
commit
0278521685

+ 32 - 33
src/App.tsx

@@ -3,48 +3,47 @@ import { BrowserRouter as Router, Switch, Route, Redirect } from "react-router-d
 // import { Home } from "~/views/home/index";
 import { Home } from "~/views/home";
 import { User } from "~/views/user/index";
-import { Me } from "~/views/me/index";
 import { VideoList } from "~/views/videoList/index";
 import { Layout } from "~/views/Layout";
 import { VideoKindList } from "./views/videokindList";
 import { DownloadCaigong1 } from "./views/downloadCaigong1";
 import { DownloadCaigong2 } from "./views/downloadCaigong2";
-
-import {PdfImages} from './views/pdfImages'
+import { PdfImages } from './views/pdfImages'
 export function App() {
+
   return (
     <Router>
-      <Switch>
-        <Route exact path="/">
-          <VideoKindList />
-        </Route>
-        <Route path="/home">
-          <Home />
-        </Route>
-        <Route path="/user">
-          <User />
-        </Route>
-        <Route path="/pdfImages">
-          <PdfImages />
-        </Route>
-        <Route path="/videoList/:subject">
-          <VideoList />
-        </Route>
+    <Switch>
+      <Route exact path="/">
+        <VideoKindList />
+      </Route>
+      <Route path="/home">
+        <Home />
+      </Route>
+      <Route path="/user">
+        <User />
+      </Route>
+      <Route path="/pdfImages">
+        <PdfImages />
+      </Route>
+      <Route path="/videoList/:subject">
+        <VideoList />
+      </Route>
 
-        <Route path="/videoKindList">
-          <VideoKindList />
-        </Route>
+      <Route path="/videoKindList">
+        <VideoKindList />
+      </Route>
 
-        <Route path="/downloadCaigong1">
-          <DownloadCaigong1 />
-        </Route>
-        <Route path="/downloadCaigong2">
-          <DownloadCaigong2 />
-        </Route>
-        <Route path="/downloadCaigong3">
-          <DownloadCaigong2 />
-        </Route>
-      </Switch>
-    </Router>
+      <Route path="/downloadCaigong1">
+        <DownloadCaigong1 />
+      </Route>
+      <Route path="/downloadCaigong2">
+        <DownloadCaigong2 />
+      </Route>
+      <Route path="/downloadCaigong3">
+        <DownloadCaigong2 />
+      </Route>
+    </Switch>
+  </Router>
   );
 }

+ 27 - 0
src/router/index.ts

@@ -0,0 +1,27 @@
+import { Redirect, Route, Switch } from "react-router-dom";
+import React, {  ComponentType, lazy, Suspense } from 'react';
+const modules = import.meta.glob("../views/**/index.tsx");
+function RenderRouter(){
+    let pathConfig = {};
+    Object.keys(modules).forEach((filePath) => {
+      const routePath = filePath
+        // 去除 src/pages 不相关的字符
+        .replace('/src/pages/', '')
+        // 去除文件名后缀
+        .replace(/.tsx?/, '')
+        // 转换动态路由 $[foo].tsx => :foo
+        .replace(/\$\[([\w-]+)]/, ':$1')
+        // 转换以 $ 开头的文件
+        .replace(/\$([\w-]+)/, '$1')
+        // 以目录分隔
+        .split('/');
+      // 使用 lodash.set 合并为一个对象
+      pathConfig ={
+        ...pathConfig,
+        routePath:modules[filePath]
+      }
+    });
+    return pathConfig;
+}
+export default RenderRouter
+

+ 0 - 0
src/views/me/index.less → src/views/home/me/index.less


+ 0 - 0
src/views/me/index.tsx → src/views/home/me/index.tsx