Vue返回上一页保留数据
1.配置路由,代码如下export default new Router({routes: [{path: '/hello',name: 'HelloWorld',component: HelloWorld,meta: {keepAlive: true // 需要缓存}},{path: '/hello2',name: 'HelloWorld2',
·
1.配置路由,代码如下
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 }
}
}
2.在App.vue文件里修改,代码如下:
<template>
<div id="app">
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive" />
</div>
</template>
3.使用$router.back()返回,才能生效。
更多推荐
已为社区贡献1条内容
所有评论(0)