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()返回,才能生效。

Logo

前往低代码交流专区

更多推荐