vue使用中經(jīng)常需要緩存當(dāng)前頁(yè)面,比如當(dāng)從商品頁(yè)跳轉(zhuǎn)到支付頁(yè),需要緩存當(dāng)前頁(yè)面
在vue使用中,我用到的方法有兩種
1.v-if
<div v-if='isShowInfo'>主頁(yè)</div>
<data-info v-else/>
這種方式會(huì)重新渲染頁(yè)面,但不會(huì)重新加載數(shù)據(jù),如果你希望不重新渲染,可以v-if配合v-show實(shí)現(xiàn)
2.keep-alive
先展示之前的通用寫法
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
現(xiàn)在的寫法
<keep-alive :include="keepAliveComponents">
<router-view></router-view>
</keep-alive>
建議用第二種,操作更方便.
匹配首先檢查組件自身的 name 選項(xiàng),如果 name 選項(xiàng)不可用,則匹配它的局部注冊(cè)名稱 (父組件 components 選項(xiàng)的鍵值)。匿名組件不能被匹配。親測(cè)如果組件不寫name屬性,那么默認(rèn)會(huì)被緩存,所以使用第二種方法一定要寫好name
對(duì)于滾動(dòng)條位置,vue-router 提供了scrollBehavior方法,直接參照官方文檔改一下就行