路由中meta自定义keepAlive属性

返回路由缓存与不缓存:

需求:领导要求点击跳转到另一个页面后回退缓存那个位置:

1、给点击加上点击事件

 <div class="sub_icon" @click="goBack"></div>


定义该方法:

  window.history.go(-1);  //表示返回上一页




2、在router.js中设置属性

import Vue from "vue";
//引入使用路由
import VueRouter from "vue-router";
import config from "../bbqm/config";
import Home from "./views/home.vue";
import Pay from "./views/pay.vue";
import Result from "./views/result.vue";
import Info from "./views/information.vue";
Vue.use(VueRouter);

const routes = [
  //设置默认指向的路径
  {
    path: "/",
    component: Home,
    meta: { title: config.name },
  },

// 重点看这里 :  在meta 中 添加  keepAlive
  {
    path: "/result",
    component: Result,
    meta: { title: config.name ,keepAlive:true},
  },
];

// 解决导航栏中的vue-router在3.0版本以上重复点菜单报错问题
  const originalPush = VueRouter.prototype.push
  VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  // mode: 'hash',   //去掉hash  不用/#
  base: config.tag,
  mode: "history",
  routes: routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return {
        x: 0,
        y: 0,
      };
    }
  },
});

export default router;

3、在APP.vue中添加:

<template>
  <div id="app">
    <keep-alive>
<!-- meta自定义keepAlive属性决定哪些页面会被缓存 -->
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>

	<!-- 当meta中keepAlive为true的时候不会被缓存 -->
    <router-view v-if="!$route.meta.keepAlive"></router-view>
  </div>
</template>

<script>
export default {};
</script>
<style>
</style>

Ps :活到老学到老,这里我主要是应用到了分页功能 点击看详情返回来的

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值