在開發vue的項目中有遇到了這樣一個需求:一個視頻列表頁面,展現視頻名稱和是否收藏,點擊進去某一項觀看,能夠收藏或者取消收藏,返回的時候須要記住列表頁面的頁碼等狀態,同時這條視頻的收藏狀態也須要更新, 可是從其餘頁面進來視頻列表頁面的時候不緩存這個頁面,也就是進入的時候是視頻列表頁面的第一頁javascript
一句話總結一下: pageAList->pageADetail->pageAList, 緩存pageAList, 同時該視頻的收藏狀態若是發生變化須要更新, 其餘頁面->pageAList, pageAList不緩存html
在網上找了不少別人的方法,都不知足咱們的需求前端
而後咱們團隊幾我的搗鼓了幾天,還真的整出了一套方法,實現了這個需求vue
無圖無真相,用一張gif圖來看一下實現後的效果吧!!! java
操做流程:node
說明:ios
用vue-cli2的腳手架搭建了一個項目,用這個項目來講明如何實現 先來看看項目目錄git
刪除了無用的components目錄和assets目錄,新增了src/pages目錄和src/store目錄, pages頁面用來存放頁面組件, store很少說,存放vuex相關的東西,新增了server/app.js目錄,用來啓動後臺服務const express = require('express')
// const bodyParser = require('body-parser')
const app = express()
let allList = Array.from({length: 30}, (v, i) => ({
id: i,
name: '視頻' + i,
isCollect: false
}))
// 後臺設置容許跨域訪問
// 先後端都是本地localhost,因此不須要設置cors跨域,若是是部署在服務器上,則須要設置
// app.all('*', function (req, res, next) {
// res.header('Access-Control-Allow-Origin', '*')
// res.header('Access-Control-Allow-Headers', 'X-Requested-With')
// res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
// res.header('X-Powered-By', ' 3.2.1')
// res.header('Content-Type', 'application/json;charset=utf-8')
// next()
// })
app.use(express.json())
app.use(express.urlencoded({extended: false}))
// 1 獲取全部的視頻列表
app.get('/api/getVideoList', function (req, res) {
let query = req.query
let currentPage = query.currentPage
let pageSize = query.pageSize
let list = allList.slice((currentPage - 1) * pageSize, currentPage * pageSize)
res.json({
code: 0,
data: {
list,
total: allList.length
}
})
})
// 2 獲取某一條視頻詳情
app.get('/api/getVideoDetail/:id', function (req, res) {
let id = Number(req.params.id)
let info = allList.find(v => v.id === id)
res.json({
code: 0,
data: info
})
})
// 3 收藏或者取消收藏視頻
app.post('/api/collectVideo', function (req, res) {
let id = Number(req.body.id)
let isCollect = req.body.isCollect
allList = allList.map((v, i) => {
return v.id === id ? {...v, isCollect} : v
})
res.json({code: 0})
})
const PORT = 3003
app.listen(PORT, function () {
console.log('app is listening port' + PORT)
})
複製代碼
在路由配置裏面把須要緩存的路由的meta添加keepAlive屬性,值爲true, 這個想必你們都知道,是緩存路由組件的 在咱們項目裏面,須要緩存的路由是pageAList,因此這個路由的meta的keepAlive設置成true,其餘路由正常寫,路由文件src/router/index.js
以下:github
import Vue from 'vue'
import Router from 'vue-router'
import home from '../pages/home'
import pageAList from '../pages/pageAList'
import pageADetail from '../pages/pageADetail'
import pageB from '../pages/pageB'
import main from '../pages/main'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'main',
component: main,
redirect: '/home',
children: [
{
path: 'home',
name: 'home',
component: home
},
{
path: 'pageAList',
name: 'pageAList',
component: pageAList,
meta: {
keepAlive: true
}
},
{
path: 'pageB',
component: pageB
}
]
},
{
path: '/pageADetail',
name: 'pageADetail',
component: pageADetail
}
]
})
複製代碼
vuex的store.js裏面存儲一個名爲excludeComponents的數組,這個數組用來操做須要作緩存的組件vue-router
state.js
const state = {
excludeComponents: []
}
export default state
複製代碼
同時在mutations.js裏面加入兩個方法, addExcludeComponent是往excludeComponents裏面添加元素的,removeExcludeComponent是往excludeComponents數組裏面移除元素
注意: 這兩個方法的第二個參數是數組或者組件name
mutations.js
const mutations = {
addExcludeComponent (state, excludeComponent) {
let excludeComponents = state.excludeComponents
if (Array.isArray(excludeComponent)) {
state.excludeComponents = [...new Set([...excludeComponents, ...excludeComponent])]
} else {
state.excludeComponents = [...new Set([...excludeComponents, excludeComponent])]
}
},
// excludeComponent多是組件name字符串或者數組
removeExcludeComponent (state, excludeComponent) {
let excludeComponents = state.excludeComponents
if (Array.isArray(excludeComponent)) {
for (let i = 0; i < excludeComponent.length; i++) {
let index = excludeComponents.findIndex(v => v === excludeComponent[i])
if (index > -1) {
excludeComponents.splice(index, 1)
}
}
} else {
for (let i = 0, len = excludeComponents.length; i < len; i++) {
if (excludeComponents[i] === excludeComponent) {
excludeComponents.splice(i, 1)
break
}
}
}
state.excludeComponents = excludeComponents
}
}
export default mutations
複製代碼
將App.vue的router-view用keep-alive組件包裹, main.vue的路由也須要這麼包裹,這點很是重要,由於pageAList組件是從它們的router-view中匹配的
<keep-alive :exclude="excludeComponents"><som-component></some-component></keep-alive>
這個寫法你們應該不會陌生,這也是尤大神官方推薦的緩存方法, exclude屬性值能夠是組件名稱字符串(組件選項的name屬性)或者數組,表明不緩存這些組件,因此vuex裏面的addExcludeComponent是表明要緩存組件,addExcludeComponent表明不緩存組件,這裏稍微有點繞,請牢記這個規則,這樣接下來你就不會被繞進去了。
App.vue
<template>
<div id="app">
<keep-alive :exclude="excludeComponents">
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
</div>
</template>
<script> export default { name: 'App', computed: { excludeComponents () { return this.$store.state.excludeComponents } } } </script 複製代碼
main.vue
<template>
<div>
<ul>
<li v-for="nav in navs" :key="nav.name">
<router-link :to="nav.name">{{nav.title}}</router-link>
</li>
</ul>
<keep-alive :exclude="excludeComponents">
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
</div>
</template>
<script> export default { name: 'main.vue', data () { return { navs: [{ name: 'home', title: '首頁' }, { name: 'pageAList', title: 'pageAList' }, { name: 'pageB', title: 'pageB' }] } }, methods: { }, computed: { excludeComponents () { return this.$store.state.excludeComponents } }, created () { } } </script>
複製代碼
接下來的兩點設置很是重要
對於須要緩存的一級路由pageAList,添加兩個路由生命週期鉤子beforeRouteEnter
和beforeRouteLeave
import {getVideoList} from '../api'
export default {
name: 'pageAList', // 組件名稱,和組件對應的路由名稱不須要相同
data () {
return {
currentPage: 1,
pageSize: 10,
total: 0,
allList: [],
list: []
}
},
methods: {
getVideoList () {
let params = {currentPage: this.currentPage, pageSize: this.pageSize}
getVideoList(params).then(r => {
if (r.code === 0) {
this.list = r.data.list
this.total = r.data.total
}
})
},
goIntoVideo (item) {
this.$router.push({name: 'pageADetail', query: {id: item.id}})
},
handleCurrentPage (val) {
this.currentPage = val
this.getVideoList()
}
},
beforeRouteEnter (to, from, next) {
next(vm => {
vm.$store.commit('removeExcludeComponent', 'pageAList')
next()
})
},
beforeRouteLeave (to, from, next) {
let reg = /pageADetail/
if (reg.test(to.name)) {
this.$store.commit('removeExcludeComponent', 'pageAList')
} else {
this.$store.commit('addExcludeComponent', 'pageAList')
}
next()
},
activated () {
this.getVideoList()
},
mounted () {
this.getVideoList()
}
}
複製代碼
對於須要緩存的一級路由的二級路由組件pageADetail,添加beforeRouteLeave路由生命週期鉤子
在這個beforeRouteLeave鉤子裏面,須要先清除一級組件的緩存狀態,若是跳轉路由匹配到一級組件,再緩存一級組件
beforeRouteLeave (to, from, next) {
let componentName = ''
// 離開詳情頁時,將pageAList添加到exludeComponents裏,也就是將須要緩存的頁面pageAList置爲不緩存狀態
let list = ['pageAList']
this.$store.commit('addExcludeComponent', list)
// 緩存組件路由名稱到組件name的映射
let map = new Map([['pageAList', 'pageAList']])
componentName = map.get(to.name) || ''
// 若是離開的時候跳轉的路由是pageAList,將pageAList從exludeComponents裏面移除,也就是要緩存pageAList
this.$store.commit('removeExcludeComponent', componentName)
next()
}
複製代碼
自認爲用這個方案來實現緩存,最終的效果很是完美了 缺點:
項目源碼的github地址,歡迎你們克隆下載
npm install
安裝項目依賴npm run server
啓動後臺服務器監聽本地3003端口npm run dev
啓動前端項目上面的方法二級緩存就夠了 上面咱們說的是兩個頁面,二級緩存的問題,如今假設有三個頁面,A1-A2-A3,一步步點進去,要求從A3返回到A2的時候,緩存A2,再從A2返回A1的時候,緩存A1,你們能夠本身動手研究下,這裏就不寫了,其實就是上面的思路,留給你們研究,你們能夠關注個人微信公衆號,裏面有三級緩存的代碼答案。
對不起,仍是不能免俗,無論大家如何不滿,我仍是要給個人公衆號打廣告,名字很俗,前端研究中心,可是內容不俗,不按期更新優質前端內容:原創或者翻譯國外優秀教程,下面是公衆號的二維碼,歡迎你們掃碼加入,一塊兒學習和進步。