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();
 }
 });

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM