Vue切换组件实现返回后不重置数据,保留历史设置操作

https://www.jb51.net/article/191280.htm?tdsourcetag=s_pcqq_aiomsg

1、<router-view ></router-view> 外层包围<keep-alive> </keep-alive>

keep-alive作用将组件实例缓存下来

<keep-alive>
  <router-view></router-view>
</keep-alive>

2、清除组件缓存

使用keep-alive 的 exclude="组件名" 属性

<keep-alive exclude="home">
    <router-view ></router-view>
</keep-alive>

  

补充知识:vue 简单的页面刷新或者跳转页面的数据保存问题

当我们想刷新页面也保存当时的数据的时候,会遇到这样那样的问题,其实,仅仅的几行代码就什么都解决了,下面是代码(js):

var vm = new Vue({
 el:'.search_moduel',
 data:{
  history:[] //存储查询历史记录的数据
 },
 methods:{
  //刷新页面或者从别的页面进入,提取this.history的值
  getPageData:function(){
  this.history = JSON.parse(localStorage.getItem('this.history'));//使用getItem方法的前提是,你再自己需要的地方使用了setItem方法
  }
 },
 //钩子函数,在实例被创建之后被调用,调用getPageData()
 created:function(){
  this.getPageData();
 }
 });

  

原文地址:https://www.cnblogs.com/kaibindirver/p/15774573.html