Vue搜索返回上一页保留原有搜索数据

配置路由

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文件里修改,代码如下:

<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>

转载 https://blog.csdn.net/csdnlinyongsheng/article/details/114579544

posted @ 2023-02-28 10:54  窦戈  阅读(478)  评论(0编辑  收藏  举报