vue route传参保存localstorage回退前进参数保存问题解决方案

具体步骤如下:

1、挂载完成后,判断浏览器是否支持popstate

  1. mounted(){

  2. if (window.history && window.history.pushState) {

  3. history.pushState(null, null, document.URL);

  4. window.addEventListener('popstate', this.goBack, false);

  5. }

  6. },

2、页面销毁时,取消监听。否则其他vue路由页面也会被监听

  1. destroyed(){

  2. window.removeEventListener('popstate', this.goBack, false);

  3. },

3、将监听操作写在methods里面,removeEventListener取消监听内容必须跟开启监听保持一致,所以函数拿到methods里面写

  1. methods:{

  2. goBack(){

  3. this.$router.replace({path: '/'});

  4. //replace替换原路由,作用是避免回退死循环

  5. }

  6. }

4.localstorage参数保存对象分为当前参数对象和前面对象链表,加入回退,上一个页面替换当前localstorage参数;

猜你喜欢

转载自blog.csdn.net/qq_26562641/article/details/82689004