Skip to content

Commit

Permalink
perf keep-alive in nested route
Browse files Browse the repository at this point in the history
  • Loading branch information
PanJiaChen committed Jan 24, 2018
1 parent 6f2a7ce commit d431de0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
19 changes: 13 additions & 6 deletions src/store/modules/tagsView.js
Expand Up @@ -6,14 +6,21 @@ const tagsView = {
mutations: {
ADD_VISITED_VIEWS: (state, view) => {
if (state.visitedViews.some(v => v.path === view.path)) return
state.visitedViews.push({
name: view.name,
path: view.path,
title: view.meta.title || 'no-name'
})

if (view.showInVisitedViews) {
state.visitedViews.push({
name: view.name,
path: view.path,
title: view.meta.title || 'no-name'
})
}

if (!view.meta.noCache) {
state.cachedViews.push(view.name)
const cachedViews = [...state.cachedViews]
cachedViews.push(view.name)
state.cachedViews = Array.from(new Set([...cachedViews]))
}
console.log(state.cachedViews)
},
DEL_VISITED_VIEWS: (state, view) => {
for (const [i, v] of state.visitedViews.entries()) {
Expand Down
2 changes: 1 addition & 1 deletion src/views/example/table/index.vue
Expand Up @@ -8,7 +8,7 @@

<script>
export default {
name: 'TableMain',
name: 'Table',
computed: {
cachedViews() {
return this.$store.state.tagsView.cachedViews
Expand Down
19 changes: 14 additions & 5 deletions src/views/layout/components/TagsView.vue
Expand Up @@ -52,20 +52,29 @@ export default {
methods: {
generateTitle, // generateTitle by vue-i18n
generateRoute() {
if (this.$route.name) {
return this.$route
let matched = [...this.$route.matched]
matched.splice(0, 1)
matched = matched.filter(item => item.name)
if (matched) {
return matched
}
return false
},
isActive(route) {
return route.path === this.$route.path || route.name === this.$route.name
},
addViewTags() {
const route = this.generateRoute()
if (!route) {
const routes = this.generateRoute()
if (!routes) {
return false
}
this.$store.dispatch('addVisitedViews', route)
const length = routes.length
routes.forEach((item, index) => {
if (index === length - 1) {
item.showInVisitedViews = true
}
this.$store.dispatch('addVisitedViews', item)
})
},
moveToCurrentTag() {
const tags = this.$refs.tag
Expand Down

1 comment on commit d431de0

@AndsteLyc
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用这个代码目前出现了两个问题:

  • 不同层次的页面多次切换后,会出现标签和页面内容对不上的问题
  • 标签上的path没有带param。比如:path为的/detail/:sn的页面 ,进入/detail/10 后,标签上的path还是/detail/:sn

Please sign in to comment.