Skip to content

Commit

Permalink
perf: 优化replace情况下,组件内存回收逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
hangaoke1 committed Feb 11, 2020
1 parent ea28a92 commit 9aa7852
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions example/components/A.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</p>
<p class="page-link">
<router-link to="/b">前往B页面</router-link>
<!-- <div @click="goB">前往B页面</div> -->
</p>
</div>
</template>
Expand All @@ -17,6 +18,11 @@ export default {
return {
count: 0
}
},
methods: {
goB () {
this.$router.replace('/b')
}
}
}
</script>
Expand Down
6 changes: 6 additions & 0 deletions example/components/B.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</p>
<p class="page-link">
<router-link to="/a">前往A页面</router-link>
<!-- <div @click="goA">前往A页面</div> -->
</p>
</div>
</template>
Expand All @@ -17,6 +18,11 @@ export default {
return {
count: 0
}
},
methods: {
goA () {
this.$router.replace('/a')
}
}
}
</script>
Expand Down
10 changes: 7 additions & 3 deletions src/components/feb-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export default (keyName, maxPage) => {
name: 'feb-alive',
abstract: true,
methods: {
cacheClear () {
cacheClear (key) {
// Fix: replace情况下 key未变化,导致实例未被销毁
const vnode = this.cache[key]
vnode && vnode.componentInstance && vnode.componentInstance.$destroy()

if (maxPage && this.keys.length > parseInt(maxPage)) {
const oldKey = this.keys[0]
const oldVnode = this.cache[oldKey]
Expand Down Expand Up @@ -112,7 +116,7 @@ export default (keyName, maxPage) => {
remove(keys, key)
keys.push(key)
} else {
this.cacheClear()
this.cacheClear(key)
cache[key] = vnode
keys.push(key)
}
Expand All @@ -131,7 +135,7 @@ export default (keyName, maxPage) => {
remove(keys, key)
keys.push(key)
} else {
this.cacheClear()
this.cacheClear(key)
cache[key] = vnode
keys.push(key)
}
Expand Down

0 comments on commit 9aa7852

Please sign in to comment.