vue使用keep-alive

it2026-01-08  7

vue使用keep-alive将页面缓存来达到记住列表位置

当列表页点进去详细页,然后再在详细页返回到列表页,这时候列表页又重新渲染了

为了解决这种尴尬以及保留组件状态或避免重新渲染,我们需要使用keep-alive

 

APP.vue

<keep-alive> <router-view></router-view> </keep-alive>

把router-view用keep-alive包起来,这样做的话,是所有的组件都缓存了,有些页面我们不需要缓存,例如新增页面

 

activated:keep-alive 组件激活时调用

activated(){ this.getDeails(); }

deactivated:keep-alive 组件停时调用

deactivated () { this.$destroy(true) }

 

最新回复(0)