- 在路由中配置
{
path: '',
name: 'xx',
meta: { keepAlive: true },
component: xx
},
- 在App.vue中配置
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive" :key="key">
</router-view>
computed: {
key() {
return this.$route.path + Math.random();
},
},
- 独有的生命周期
//进入时触发
activated() {}
//退出时触发
deactivated() {}
keepAlive还支持很多功能,需要自己去查