Skip to content

Commit d431de0

Browse files
committed
perf keep-alive in nested route
1 parent 6f2a7ce commit d431de0

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/store/modules/tagsView.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ const tagsView = {
66
mutations: {
77
ADD_VISITED_VIEWS: (state, view) => {
88
if (state.visitedViews.some(v => v.path === view.path)) return
9-
state.visitedViews.push({
10-
name: view.name,
11-
path: view.path,
12-
title: view.meta.title || 'no-name'
13-
})
9+
10+
if (view.showInVisitedViews) {
11+
state.visitedViews.push({
12+
name: view.name,
13+
path: view.path,
14+
title: view.meta.title || 'no-name'
15+
})
16+
}
17+
1418
if (!view.meta.noCache) {
15-
state.cachedViews.push(view.name)
19+
const cachedViews = [...state.cachedViews]
20+
cachedViews.push(view.name)
21+
state.cachedViews = Array.from(new Set([...cachedViews]))
1622
}
23+
console.log(state.cachedViews)
1724
},
1825
DEL_VISITED_VIEWS: (state, view) => {
1926
for (const [i, v] of state.visitedViews.entries()) {

src/views/example/table/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<script>
1010
export default {
11-
name: 'TableMain',
11+
name: 'Table',
1212
computed: {
1313
cachedViews() {
1414
return this.$store.state.tagsView.cachedViews

src/views/layout/components/TagsView.vue

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,29 @@ export default {
5252
methods: {
5353
generateTitle, // generateTitle by vue-i18n
5454
generateRoute() {
55-
if (this.$route.name) {
56-
return this.$route
55+
let matched = [...this.$route.matched]
56+
matched.splice(0, 1)
57+
matched = matched.filter(item => item.name)
58+
if (matched) {
59+
return matched
5760
}
5861
return false
5962
},
6063
isActive(route) {
6164
return route.path === this.$route.path || route.name === this.$route.name
6265
},
6366
addViewTags() {
64-
const route = this.generateRoute()
65-
if (!route) {
67+
const routes = this.generateRoute()
68+
if (!routes) {
6669
return false
6770
}
68-
this.$store.dispatch('addVisitedViews', route)
71+
const length = routes.length
72+
routes.forEach((item, index) => {
73+
if (index === length - 1) {
74+
item.showInVisitedViews = true
75+
}
76+
this.$store.dispatch('addVisitedViews', item)
77+
})
6978
},
7079
moveToCurrentTag() {
7180
const tags = this.$refs.tag

0 commit comments

Comments
 (0)