<!--more-->html
vue-router
组件,经过this.$router.push({path: path, query: query});
方式,将页码
和选择条件
做为参数存储在url中,这种方式在上面的UI设计中是可行的,可是当列表页中包含tab组件时(分页组件是公用的),会由于push的因素(由于push会打开新页面)致使一些问题(PS:也多是本人技术能力的缘由),未实现。History API
(HTML5开始支持),经过history.replaceState
方式,将页码
做为参数存储在url中,将选择条件
存储在history中(尚不清楚数据具体是存储在哪里);经过location.hash
方式获取页码
;经过history.state
方式获取存储的选择条件。为分页组件添加一个开关(openroute),由于须要灰度上线,万一有问题,要调整的页面也只有一个。代码以下:vue
<script> export default { props: { openroute: { type: Boolean, default: () => (true) } }, } </script>
页码
和选择条件
&获取页码
<script> export default { methods: { fetchData(page) { //请求参数 let params = this.params; //请求页码 let newPage; //openroute处理 if (this.openroute) { //为url添上#page if (page) { history.replaceState(params.data, document.title, "#" + page); } else { if (history.state) { if (!history.state.key && location.hash && location.hash.split("#") && location.hash.split("#")[1]) { if (JSON.stringify(history.state) !== JSON.stringify(params.data)) { //选择条件变动则请求第一页 history.replaceState(params.data, document.title, "#1"); } else { history.replaceState(params.data, document.title, "#" + location.hash.split("#")[1]); } } else { history.replaceState(params.data, document.title, "#1"); } } else { if (location.hash && location.hash.split("#") && location.hash.split("#")[1]) { history.replaceState(params.data, document.title, "#" + location.hash.split("#")[1]); } else { history.replaceState(params.data, document.title, "#1"); } } } //获取url后面的#page if (location.hash && location.hash.split("#") && location.hash.split("#")[1]) { newPage = Number(location.hash.split("#")[1]); } else { newPage = 1; } } else { newPage = page; } //请求数据,得到结果,传递给列表页面 } } } </script>
选择条件
目前多是由于框架设计的问题,无法在请求数据以前经过Object.assign
方式为替换初始变量,因此先这样处理(笨方法,各位大佬有解决办法麻烦指导下,谢谢):vue-router
<script> export default { data() { return { form: { aaa: (history.state && history.state.aaa) ? history.state.aaa : null, bbb: (history.state && history.state.bbb) ? history.state.bbb : null, ccc: (history.state && history.state.ccc) ? history.state.ccc : null }, }; } }; </script>
已解决,初始变量不须要动,能够经过如下方式实现:浏览器
created(){ //获取缓存的数据 if (history.state) { Object.assign(this.form, history.state) if (this.form.key) { delete this.form.key } } },
这里记录下:以前觉得created方法是在分页组件的watch监听以后执行的,后来发现被误导了(由于以前是经过Object.assign(true, this.form, history.state)
方式实现数据赋值的,可是并无成功)。下面作个小总结:缓存
结论:前者:改a不影响b;后者:改a影响b框架
分析(这篇文章有源码分析(<font color='red'>求解答:WebStorm中如何关联源码?</font>),很棒):https://www.cnblogs.com/libin...源码分析
history.replaceState
方式是由于:它不会将更改后的url压到history栈中,因此不会增长回退和前进的操做步数;history.replaceState
方式,可存储的state大小不能操做640k;由于是公司项目,因此目前没有Demo或源码fetch