antd 刷新页面及后退时菜单栏高亮问题


左侧菜单刷新无法高亮用 withRouter,设置selectedKeys属性

   

import { Link, Route, Switch, Redirect, HashRouter, withRouter } from 'react-router-dom';

const LeftSider = withRouter(({ history }) => {
  return (
    <Sider >
      <Menu
        mode="inline"
        defaultSelectedKeys={['/new-pro']}
        selectedKeys={[history.location.pathname]}
        theme="dark"
      >
        <Menu.Item key="/new-pro">
          <Link to="/new-pro" replace />
          新建项目
        </Menu.Item>
        <Menu.Item key="/pro-list">
          <Link to="/pro-list" replace />
          项目列表
        </Menu.Item>
      </Menu>
    </Sider>

  );
})

class App extends Component {
  render() {
    return (
      <HashRouter>
        <Layout style={{ minHeight: '100vh' }}>
          <LeftSider />
          <Layout>
            <Header style={{ background: '#fff', padding: 0 }}>
            </Header>
            <Content style={{ margin: '0 16px' }}>
              <div
                style={{
                  margin: '24px 16px',
                  padding: 0,
                  background: '#fff',
                  minHeight: 850,
                }}   >
                <main>
                  <Switch>
                    <Route path="/new-pro" component={History} />
                    <Route path="/pro-list" component={HeaderBar} />
                    <Redirect to="/new-pro" />
                  </Switch>
                </main>
              </div>
            </Content>
          </Layout>
        </Layout>
      </HashRouter>
    );
  }
}
export default App;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM