配置路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
export default new Router({
routes: [
{
path: '/hello',
name: 'HelloWorld',
component: HelloWorld,
meta: {
keepAlive: true // 需要缓存
}
},
{
path: '/hello2',
name: 'HelloWorld2',
component: HelloWorld2,
meta: {
keepAlive: false // 不需要缓存
}
}
],
mode: 'history',
scrollBehavior (to, from, savedPosition) {
if (savedPosition) {
return savedPosition
} else {
return { x: 0, y: 0 }
}
}

在App.vue文件里修改

1
2
3
4
5
6
7
8
9
<template>
<div id="app">
<!-- <router-view /> -->
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive" />
</div>
</template>

使用$router.back()返回,才能生效。