Skip to content

Commit

Permalink
wip(router): 🚀 [App] add route
Browse files Browse the repository at this point in the history
  • Loading branch information
jsxiaosi committed Nov 21, 2022
1 parent 97a884b commit 1bb2274
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 25 deletions.
27 changes: 3 additions & 24 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
import { useState } from 'react';
import reactLogo from './assets/react.svg';
import { RouterProvider } from 'react-router-dom';
import './App.css';
import router from './router';

function App() {
const [count, setCount] = useState(0);

return (
<div className="App">
<div>
<a href="https://vitejs.dev" target="_blank" rel="noreferrer">
<img src="/vite.svg" className="logo" alt="Vite logo" />
</a>
<a href="https://reactjs.org" target="_blank" rel="noreferrer">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>count is {count}</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">Click on the Vite and React logos to learn more</p>
</div>
);
return <RouterProvider router={router} />;
}

export default App;
13 changes: 13 additions & 0 deletions src/router/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { lazy } from 'react';
import { createBrowserRouter } from 'react-router-dom';

const Home = lazy(() => import('@/views/Home'));
const ErrorPage403 = lazy(() => import('@/views/error/403'));

export default createBrowserRouter([
{
path: '/',
element: <Home />,
errorElement: <ErrorPage403 />,
},
]);
33 changes: 33 additions & 0 deletions src/views/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState } from 'react';
import reactLogo from '@/assets/react.svg';

const Home = () => {
const [count, setCount] = useState(0);

const render = () => {
return (
<div className="App">
<div>
<a href="https://vitejs.dev" target="_blank" rel="noreferrer">
<img src="/vite.svg" className="logo" alt="Vite logo" />
</a>
<a href="https://reactjs.org" target="_blank" rel="noreferrer">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount((count) => count + 1)}>count is {count}</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">Click on the Vite and React logos to learn more</p>
</div>
);
};

return render();
};

export default Home;
7 changes: 7 additions & 0 deletions src/views/error/403.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default () => {
const render = () => {
return <>403</>;
};

return render();
};
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"baseUrl": ".",
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
"jsx": "react-jsx",
"paths": {
"@/*": ["src/*"],
"#/*": ["types/*"]
}
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
Expand Down
12 changes: 12 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

Expand All @@ -7,4 +8,15 @@ export default defineConfig({
server: {
host: true,
},
resolve: {
// 引用别名配置
alias: {
// 配置@别名
'@': `${path.resolve(__dirname, 'src')}`,
// 配置#别名
'#': `${path.resolve(__dirname, 'types')}`,
},
// 导入时想要省略的扩展名列表。注意,不 建议忽略自定义导入类型的扩展名(例如:.vue),因为它会干扰 IDE 和类型支持。
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
},
});

0 comments on commit 1bb2274

Please sign in to comment.