效果图
Router配置
设置根路径
router.js
文件, 其中 <Route path='/' exact component={IndexPage} />
中的 exact
要注释掉,否则将无法跳转其他链接。
1 2 3 4 5 6 7 8 9 10 11 12 13
| import React from 'react'; import { Router, Route, Switch } from 'dva/router'; import IndexPage from './pages/IndexPage'; function RouterConfig({ history }) { return ( <Router history={history}> <Switch> <Route path='/' component={IndexPage} /> {} </Switch> </Router> ); } export default RouterConfig;
|
路由地址
在 IndexPage.js
设置相关路由,并导入导航栏。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import Home from './Home' import Menu from './Menu'
function IndexPage(props) { return ( <Content className={styles.content}> <Switch> <Route path='/home' component={Home}/> <Route path='/manage' component={Manage}/> <Redirect to='/home'/> {} </Switch> </Content> ) }
|
路由跳转
在 NavBar
组件设置跳转。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| class NavBar extends Component { constructor(props) { super(props); this.state = { selectedKeys: [] } } componentDidMount() { this.onHandleSelKey(this.props.location.pathname) } componentWillReceiveProps(nextProps) { const {pathname: preName} = this.props.location; const {pathname: nextName} = nextProps.location; if(preName !== nextName) { this.onHandleSelKey(nextName) } } onHandleSelKey(pathname) { const temp = pathname.split('/'); const keys = temp.length < 2 ? 'home' : temp[1]; this.setState({selectedKeys: [keys]}) } render() { return ( <Menu className={styles['menu-left']} mode={'horizontal'} defaultSelectedKeys={['home']} selectedKeys={this.state.selectedKeys} > <Menu.Item key={'home'}><Link to={'/home'}>主页</Link></Menu.Item> <Menu.Item key={'manage'}><Link to={'/manage'}>管理</Link></Menu.Item> </Menu> ) } }
|
Router优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
const RouteConfig = [ { path: '/', component: IndexPage, routes: [ { path: '/home', component: Home, redirect: true, isAuthority: true, }, { path: '/manage', component: Manage, isAuthority: true, } ] } ]
function RouterConfig({ history }) { return ( <Router history={history}> <Switch> {} {} {RouteConfig.map((route, i) => <SubRoutes {...route} key={i} /> )} </Switch> </Router> ); }
function IndexPage(props) { return ( <Switch> {routes.map((route, i) => <SubRoutes {...route} key={i} />)} {
} {} <RedirectRoute exect={true} form={'/'} routes={routes} />
{
} <NoMatchRoute /> </Switch> ) }
/** SubRoutes.js */ export default function SubRoutes({routes, component: Component}) { return ( <Route render={props => <Component {...props} routes={routes} />} /> ) }
export function RedirectRoute({routes, from, exact}) { const router = routes.filter(item => item.redirect); const to = router.length ? router[0].path : routes[0].path; return <Redirect exact={exact} from={from} to={to}/> }
export function NoMatchRoute({ status=404 }) { return <Route render={props => <NoMatch {...props} status={status} />} /> }
|