随着vue3.0的发布,vue-router发布了4.0版本,文档 很明了,提供了vue2路由到vue3的变化和写法指导。

  vue2:

// transition
<transition name="van-fade">
<router-view />
</transition> // keep-alive
<keep-alive>
<router-view v-if="this.$route.meat.keepAlive"></router-view>
</keep-alive>
<keep-alive v-if="!this.$router.meta.keepAlive"></keep-alive>

  vue3:

  <router-view v-slot="{ Component, route }">
<transition :name="route.meta.transitionName">
<keep-alive v-if="route.meta.keepAlive">
<component :is="Component" />
</keep-alive>
<component :is="Component" v-else />
</transition>
</router-view>

  需要使用 v-slot API来传入渲染的comp和route对象,而不再用this.$route

  route.js写法大体没啥变化,写在最后的未匹配上路由的rediect,需要用正则来匹配

  {
// path: '/*',
path: '/:pathMatch(.*)*',
redirect: '/',
},

  监听路由前进后退实现transtion的动画效果,查了许多资料都有点不太完善,有用数组存住history 每次手动push和pop然后比对的,有用storage的,有用子路由‘/’来对比的...

  我的方式:

  

// route
router.beforeEach((to, from) => { const toDepth = to.path.split('/').length;
const fromDepth = from.path.split('/').length; to.meta.transitionName =
toDepth > fromDepth ?
'slide-left' :
(to.path === store.state.pushPath ?
'slide-left' :
'slide-right'); }); // store
state: {
pushPath: '',
},
mutations: {
setPushPath(state, payload) {
state.pushPath = payload;
},
}, // util
export const push = (path: string, params?: Record<string, string | number>): void => {
store.commit('setPushPath', path);
router.push({ path, params });
};

  每次跳转使用自己简单封装的路由api,记录是前进还是后退。 可还行


  更新一下,这种 v-if 的方式,其实是有问题的,太久没登录博客了,项目里面已经发现了问题改用 include 方式,没有更新到文章。

  原因是一旦有新打开的页面 , 会重新加载keep-alive组件 , 丢失所有缓存,导致回退页面的时候还是会走mounted,重新加载页面。

  那么,就用 include 吧!

  执行 watch 路由的操作,可以在app.vue里面写watch方式,也可以就在route里面的 beforeEach 方法里面去修改数组,通过store来绑定传值。

// route
router.beforeEach((to, from, next) => { const toDepth = to.path.split('/').length;
const fromDepth = from.path.split('/').length; // to.meta.transitionName =
// toDepth > fromDepth ?
// 'van-slide-left' :
// (to.path === store.state.pushPath ?
// 'van-slide-left' :
// 'van-slide-right');
const isPush = toDepth > fromDepth || to.path === store.state.pushPath;
to.meta.transitionName = isPush ? 'van-slide-left' : 'van-slide-right'; if (to.meta.keepAlive) {
store.commit('addIncludes', to.name);
} if (from.meta.keepAlive && !isPush) {
store.commit('minusIncludes', from.name);
} next();
}); // store
state: {
pushPath: '',
include: []
},
mutations: {
setPushPath(state, payload) {
state.pushPath = payload;
},
addIncludes(state, payload) {
if (!state.include.includes(payload)) {
state.include.push(payload);
}
},
minusIncludes(state, payload) {
const index = state.include.indexOf(payload);
if (index !== -1) {
state.include.splice(index, 1);
}
},
},

vue3使用路由keep-alive和监听路由实现transition的更多相关文章

  1. Vue2路由跳转传参,获取路由参数,Vue监听路由

    1 this.$router.push({ 2 // name:路由组件名 3 name: routeName, 4 query: { 5 mapId:this.mapId 6 } 7 }) 8 9 ...

  2. vue 在.vue文件里监听路由

    监听路由  watch   $route vue项目中的App.vue 文件 <template> <div id="app"> <!--includ ...

  3. vue监听路由的变化,跳转到同一个页面时,Url改变但视图未重新加载问题

    引入:https://q.cnblogs.com/q/88214/ 解决方法: 添加路由监听,路由改变时执行监听方法 methods:{ fetchData(){ console.log('路由发送变 ...

  4. AngularJS监听路由变化

    使用AngularJS时,当路由发生改变时,我们需要做某些处理,此时可以监听路由事件,常用的是$routeStartChange, $routeChangeSuccess.完整例子如下: <!D ...

  5. angular的路由和监听路由的变化和用户超时的监听

    先看两篇博客:http://camnpr.com/javascript/1652.html 这一篇博客是重点中的重点:                   http://www.tuicool.com ...

  6. angular 全局 监听路由变化

    app.run(['$rootScope', '$location', function($rootScope, $location) { /* 监听路由的状态变化 */ $rootScope.$on ...

  7. Angular 监听路由变化事件

    摘要: $stateChangeStart- 当模板开始解析之前触发 $rootScope.$on('$stateChangeStart', function(event, toState, toPa ...

  8. Angular 监听路由变化

    var app = angular.module('Mywind',['ui.router']) //Angular 监听路由变化 function run($ionicPlatform, $loca ...

  9. vue中监听路由参数变化

    今天遇到一个这样的业务场景:在同一个路由下,只改变路由后面的参数值, 比如在这个页面  /aaa?id=1 ,在这个页面中点击一个按钮后 跳转到 /aaa?id=2 , 但从“/aaa?id=1”到“ ...

  10. vue 如何通过监听路由变化给父级路由菜单添加active样式

    1.项目需求:在项目开发中,多级菜单的情况下,勾选子菜单时,需要在父级菜单添加active样式. 2.遇到的问题:一级路由菜单的话,点击当前路由会自动在路由标签上添加router-link-exact ...

随机推荐

  1. TienChin-课程管理-数据表创建

    CREATE TABLE `tienchin_course` ( `course_id` int NOT NULL AUTO_INCREMENT COMMENT '课程ID', `type` int ...

  2. C++ CryptoPP使用RSA加解密

    Crypto++ (CryptoPP) 是一个用于密码学和加密的 C++ 库.它是一个开源项目,提供了大量的密码学算法和功能,包括对称加密.非对称加密.哈希函数.消息认证码 (MAC).数字签名等.C ...

  3. 【可观测性系列】 Opentelemetry 介绍

    作者简介:大家好,我是蓝胖子 ️博客首页:博客园主页蓝胖子的编程梦 每日一句:人生的烦恼,多在于明白的太多,而做的太少 大家好,我是蓝胖子,随着微服务的流行,服务的可观测性概念被越来越多人提及到,究竟 ...

  4. 【进阶篇】Java 实际开发中积累的几个小技巧(一)

    目录 前言 一.枚举类的注解 二.RESTful 接口 三.类属性转换 四.Stream 流 五.判空和断言 5.1判空部分 5.2断言部分 文章小结 前言 笔者目前从事一线 Java 开发今年是第 ...

  5. 简单总结Tomcat/SpringMVC/Spring/SpringBoot的关系

    Tomcat是一个Web应用服务器,可以作为Servlet容器.它的作用是,解析客户端client发起的request,并组装出HttpRequest.创建HttpResponse,将二者交于内部的H ...

  6. .NET Core开发实战(第3课:.NET Core的现状、未来以及环境搭建)--学习笔记

    03 | .NET Core的现状.未来以及环境搭建 .NET Core的现状 .NET Core 的应用场景:桌面端.Web端.云端.移动端.游戏.IOT 和 AI 云端指的是 .NET Core ...

  7. 扒开源安卓性能测试工具moblieperf源码——开发属于你自己的性能稳定性测试工具

    moblieperf下载和使用 moblieperf由阿里巴巴开源的Android性能测试工具 下载:官方源码地址mobileperf github 使用: 使用pycharm打开下载的项目 使用只需 ...

  8. Linux进程与线程的基本概念及区别

    前言 假设你正在玩一款在线多人游戏,在游戏中,有多个角色需要进行不同的操作,例如攻击.移动.释放技能等等. 接下来,我们用玩游戏的例子,来解释进程和和线程的概念,以及进程和线程的区别. 进程的基本概念 ...

  9. NodeJs web项目框架Express笔记

    安装 以下都使用Yarn进行. 环境前提: 已经安装NodeJS(及自带的npm), 已经安装Yarn # 全局安装 yarn global add express-generator@4 #查看版本 ...

  10. 【Unity3D】导航系统

    1 导航系统简介 ​ 导航系统用于智能避障并寻找目标物体,如:王者荣耀中,当玩家跑到敌方塔的攻击范围内,敌方塔就会发射火团攻击玩家,当玩家逃跑时,火团会智能跟随玩家,其中智能跟随就使用到了导航系统. ...