react-router(v3)切换页面时不刷新页面,实现显示和隐藏子路由组件
2018-04-04 20:32
1281 查看
v4版本应该也差不多的解决方法
思路很简单:就是在{this.props.children}渲染的地方进行处理,根据当前路由名称判断组件显示与隐藏。
思路很简单:就是在{this.props.children}渲染的地方进行处理,根据当前路由名称判断组件显示与隐藏。
children即this.props.children; 将children保存到全局windows中 save = (children) => { this.routeName = children.props.route.name; if (!window.routeCache) window.routeCache = {}; if (!window.routeCache[this.path]) window.routeCache[this.path] = {}; if (!window.routeCache[this.path][this.routeName]) { window.routeCache[this.path][this.routeName] = children; } }
然后在{this.props.children}的位置使用下面的方法,判断当前路由选择对应组件进行显示和隐藏 render = () => { return Object.keys(window.routeCache[this.path]).map((key) => { if (key === this.routeName) return <div key={key} style={{height: '100%'}}>{window.routeCache[this.path][key]}</div>; return <div key={key} style={{display: 'none'}}>{window.routeCache[this.path][key]}</div>; }); }
相关文章推荐
- React-Router做路由,打包出来的静态文件丢到Tomcat里路由不生效,刷新页面404两个错误
- 使用react-router-dom优雅的实现页面(路由)跳转, 而且保持当前页面状态
- 利用JS实现:页面组件的动态显示、隐藏
- 解决element-ui中el-menu组件作为vue-router模式在刷新页面后default-active属性与当前路由页面不一致问题的方法
- 当多个<router />使用同一个组件的时候,切换页面地址,页面不刷新的问题
- (React-Native 学习之六) react-native-router-flux 组件学习(页面间的跳转)
- 原生JS实现全屏切换以及导航栏滑动隐藏及显示——重构前
- 框架页面的实现及Javascript的改变页面显示和隐藏
- vue-router同一路由地址同页面切换无效解决
- jquery 实现背景图片循环切换,显示隐藏div
- react-router与react-reduct配合使用时页面不刷新问题解决方法
- vue-router实现webApp切换页面动画效果代码
- 页面向下滑动实现div显示和隐藏
- react-router 路由控制页面跳转
- Android组件TabHost实现页面中多个选项卡切换效果
- 同一页面tab切换对应显示内容分页实现
- 用jquery实现隐藏列表表单的显示关闭切换以及Ajax方式改动提交相应的那一行的改动内容。
- Vue-router 切换组件页面时进入进出动画方法
- vue-router中关于组件复用页面不刷新的问题
- 用jquery实现隐藏列表表单的显示关闭切换以及Ajax方式修改提交对应的那一行的修改内容。