1 /router/index.js
配置路由元信息keepAlive: true
{ path: "user", name: "用户管理", component: User, meta: { requireAuth: true, keepAlive: true // 用户列表需要被缓存 } },
2 /app.vue
<keep-alive> <router-view v-if="$route.meta.keepAlive"></router-view> </keep-alive> <router-view v-if="!$route.meta.keepAlive"></router-view>
user路由的$route.meta.keepAlive为true,便达到了被缓存的效果。
文章来源: Vue项目中使用keep-Alive步骤