Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

antd 实现keep alive #6

Open
raclen opened this issue Sep 15, 2019 · 0 comments
Open

antd 实现keep alive #6

raclen opened this issue Sep 15, 2019 · 0 comments
Labels
博客搬迁 以前博客的文章迁移过来

Comments

@raclen
Copy link
Owner

raclen commented Sep 15, 2019

背景:后台项目需要切换路由的时候保存用户数据
就琢磨着怎么实现
antd pro项目
gitbub找了个钩子

npm install react-live-route --save-dev

找到这个文件
BasicLayout.js

   {getRoutes(match.path, routerData).map(item => (
                    <AuthorizedRoute
                        key={item.key}
                        path={item.path}
                        component={item.component}
                        exact={item.exact}
                        authority={item.authority}
                        redirectPath="/exception/403"
                    />
                ))}

修改为

<ScrollToTop>
                {getRoutes(match.path, routerData).map((item, index) => {
                  return (<LiveRoute path={item.path} key={index} alwaysLive={needAlwaysLive} component={item.component} />)
                })}
</ScrollToTop>

ScrollToTop.js
切换的时候定位到顶部

import { withRouter} from 'dva/router';
import React, { Component } from 'react';
class ScrollToTop extends Component {
  componentDidUpdate(prevProps) {
    if (this.props.location !== prevProps.location) {
      window.scrollTo(0, 0)
    }
  }

  render() {
    return this.props.children
  }
}

export default withRouter(ScrollToTop)

#2018-12-06

@raclen raclen added the 博客搬迁 以前博客的文章迁移过来 label Sep 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
博客搬迁 以前博客的文章迁移过来
Projects
None yet
Development

No branches or pull requests

1 participant