欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中進(jìn)入詳情頁記住滾動(dòng)位置的方法(keep-alive)

 更新時(shí)間:2018年09月21日 11:12:04   作者:徹夜不歸  
今天小編就為大家分享一篇vue中進(jìn)入詳情頁記住滾動(dòng)位置的方法(keep-alive),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

> 有時(shí)業(yè)務(wù)提出這樣一個(gè)需求 就是從商品頁面進(jìn)入到列表詳情頁 要保存當(dāng)前滾動(dòng)的位置,這里我就想到了keep-alive

1.首先在路由中引入需要的模塊

{ 
path: ‘/scrollDemo', 
name: ‘scrollDemo', 
meta: { 
keepAlive: true // 需要緩存 
}, 
component: resolve => { require([‘../view/scrollDemo.vue'], resolve) } 
}

2.在App.vue中設(shè)置緩存組件

  <keep-alive>  // 緩存組件跳轉(zhuǎn)的頁面
    <router-view v-if="$route.meta.keepAlive" class="ui-view" transition-mode="out-in"></router-view>
  </keep-alive> 

 // 非緩存組件跳轉(zhuǎn)頁面
 <router-view v-if="!$route.meta.keepAlive" class="ui-view" transition-mode="out-in"></router-view>

3.在頁面注冊對應(yīng)的事件

1. 在return中定義一個(gè)初始值 scroll

2. 在mouted中 ,mouted中的方法代表dom已經(jīng)加載完畢

window.addEventListener('scroll', this.handleScroll);

3.methods 用于存放頁面函數(shù)

   handleScroll () {
    this.scroll = document.documentElement && document.documentElement.scrollTop

    console.log(this.scroll)
   }

4. activated 為keep-alive加載時(shí)調(diào)用

   activated() {
     if(this.scroll > 0){
      window.scrollTo(0, this.scroll);
      this.scroll = 0;
      window.addEventListener('scroll', this.handleScroll);
     }
  }

5.deactivated 頁面退出時(shí)關(guān)閉事件 防止其他頁面出現(xiàn)問題

  deactivated(){
   window.removeEventListener('scroll', this.handleScroll);
  }

以上這篇vue中進(jìn)入詳情頁記住滾動(dòng)位置的方法(keep-alive)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論