add sample usage of config router

This commit is contained in:
syy11cn 2021-10-29 17:38:30 +08:00
parent 974feace40
commit ffd5c32f23
6 changed files with 49 additions and 12 deletions

View File

@ -30,7 +30,7 @@
"react-dom": "^17.0.0" "react-dom": "^17.0.0"
}, },
"devDependencies": { "devDependencies": {
"@syy11cn/config-router": "^1.0.4", "@syy11cn/config-router": "^1.0.5",
"@types/react": "^17.0.33", "@types/react": "^17.0.33",
"@types/react-dom": "^17.0.10", "@types/react-dom": "^17.0.10",
"@types/react-router-dom": "^5.3.2", "@types/react-router-dom": "^5.3.2",

View File

@ -1,13 +1,25 @@
import React from 'react'; import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import './index.css'; import './index.css';
import App from './App';
import { BrowserRouter as Router } from 'react-router-dom'; import { BrowserRouter as Router } from 'react-router-dom';
import { RouterView } from '@syy11cn/config-router';
import routes from './routes';
ReactDOM.render( ReactDOM.render(
<React.StrictMode> <React.StrictMode>
<Router> <Router>
<App /> <RouterView
routes={routes}
onEnter={function (to: string, next: (path?: string) => void): void {
// Let '/' and '/404' go.
if (to === '/' || to === '/404') {
next();
return;
}
// Other routes would be redirected to '/404'
next('/404');
}}
></RouterView>
</Router> </Router>
</React.StrictMode>, </React.StrictMode>,
document.getElementById('root'), document.getElementById('root'),

View File

@ -1,5 +1,17 @@
import { routeType } from '@syy11cn/config-router'; import { routeType } from '@syy11cn/config-router';
import App from '@/App';
import ErrorPage from '@/views/ErrorPage';
const routes: Array<routeType> = []; const routes: Array<routeType> = [
{
path: '/404',
component: ErrorPage,
},
{
path: '/',
exact: true,
component: App,
},
];
export default routes; export default routes;

7
src/views/ErrorPage.tsx Normal file
View File

@ -0,0 +1,7 @@
import * as React from 'react';
const ErrorPage: React.FC = () => {
return <h1>404 Not Found</h1>;
};
export default ErrorPage;

View File

@ -1,7 +1,13 @@
import { defineConfig } from 'vite' import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react' import react from '@vitejs/plugin-react';
import path from 'path';
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
plugins: [react()] plugins: [react()],
}) resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
});

View File

@ -363,10 +363,10 @@
estree-walker "^2.0.1" estree-walker "^2.0.1"
picomatch "^2.2.2" picomatch "^2.2.2"
"@syy11cn/config-router@^1.0.4": "@syy11cn/config-router@^1.0.5":
version "1.0.4" version "1.0.5"
resolved "https://registry.yarnpkg.com/@syy11cn/config-router/-/config-router-1.0.4.tgz#da614073cce0837039d00b978389f5baa3eb4765" resolved "https://registry.yarnpkg.com/@syy11cn/config-router/-/config-router-1.0.5.tgz#9b8d4761069bb4051ec1d5973142fdd8783dbf8f"
integrity sha512-5+pSqHZdX3CSfERG7BJ809xYs3mWh8Okt7uF0rqT6ppH1qqYBMhPwih/YyNIoSdbjAo+vhu+QTWwWGpcKS7igg== integrity sha512-/wqJhEHArxtaSY24LePVZ3HzlKzYjlywO9I8i8r/AaasgV3C8+0eDsiTCwgdYTE+QkrxhG5UEEMX/XKT5jQ+aQ==
dependencies: dependencies:
react-router-dom "^5.3.0" react-router-dom "^5.3.0"