Skip to content

Commit cea2100

Browse files
committed
NotFound component added for not exist routes.
NotFound component added for not exist routes.
1 parent 689c071 commit cea2100

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

reactjs/src/components/Layout/AppLayout.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import './AppLayout.less';
22

33
import * as React from 'react';
44

5-
import { Redirect, Switch } from 'react-router-dom';
5+
import { Redirect, Switch, Route } from 'react-router-dom';
66

77
import DocumentTitle from 'react-document-title';
88
import Footer from '../../components/Footer';
@@ -12,6 +12,7 @@ import ProtectedRoute from '../../components/Router/ProtectedRoute';
1212
import SiderMenu from '../../components/SiderMenu';
1313
import { appRouters } from '../Router/router.config';
1414
import utils from '../../utils/utils';
15+
import NotFoundRoute from '../Router/NotFoundRoute';
1516

1617
const { Content } = Layout;
1718

@@ -48,13 +49,18 @@ class AppLayout extends React.Component<any> {
4849
</Layout.Header>
4950
<Content style={{ margin: 16 }}>
5051
<Switch>
52+
{this.props.location.pathname === '/' && <Redirect from="/" to="/dashboard" />}
5153
{appRouters
5254
.filter((item: any) => !item.isLayout)
5355
.map((route: any, index: any) => (
54-
<ProtectedRoute key={index} path={route.path} component={route.component} permission={route.permission} />
56+
<Route
57+
exact
58+
key={index}
59+
path={route.path}
60+
render={props => <ProtectedRoute component={route.component} permission={route.permission} />}
61+
/>
5562
))}
56-
57-
<Redirect from="/" to="/dashboard" />
63+
{pathname !== '/' && <NotFoundRoute />}
5864
</Switch>
5965
</Content>
6066
<Layout.Footer style={{ textAlign: 'center' }}>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React, { Component } from 'react';
2+
import { Route, Redirect } from 'react-router-dom';
3+
4+
export class NotFoundRoute extends Component {
5+
render() {
6+
return (
7+
<>
8+
<Route
9+
render={props => {
10+
return (
11+
<Redirect
12+
to={{
13+
pathname: '/exception?type=404',
14+
state: { from: props.location },
15+
}}
16+
/>
17+
);
18+
}}
19+
/>
20+
</>
21+
);
22+
}
23+
}
24+
25+
export default NotFoundRoute;

0 commit comments

Comments
 (0)