vue3使用路由keep-alive和监听路由实现transition
随着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的更多相关文章
- Vue2路由跳转传参,获取路由参数,Vue监听路由
1 this.$router.push({ 2 // name:路由组件名 3 name: routeName, 4 query: { 5 mapId:this.mapId 6 } 7 }) 8 9 ...
- vue 在.vue文件里监听路由
监听路由 watch $route vue项目中的App.vue 文件 <template> <div id="app"> <!--includ ...
- vue监听路由的变化,跳转到同一个页面时,Url改变但视图未重新加载问题
引入:https://q.cnblogs.com/q/88214/ 解决方法: 添加路由监听,路由改变时执行监听方法 methods:{ fetchData(){ console.log('路由发送变 ...
- AngularJS监听路由变化
使用AngularJS时,当路由发生改变时,我们需要做某些处理,此时可以监听路由事件,常用的是$routeStartChange, $routeChangeSuccess.完整例子如下: <!D ...
- angular的路由和监听路由的变化和用户超时的监听
先看两篇博客:http://camnpr.com/javascript/1652.html 这一篇博客是重点中的重点: http://www.tuicool.com ...
- angular 全局 监听路由变化
app.run(['$rootScope', '$location', function($rootScope, $location) { /* 监听路由的状态变化 */ $rootScope.$on ...
- Angular 监听路由变化事件
摘要: $stateChangeStart- 当模板开始解析之前触发 $rootScope.$on('$stateChangeStart', function(event, toState, toPa ...
- Angular 监听路由变化
var app = angular.module('Mywind',['ui.router']) //Angular 监听路由变化 function run($ionicPlatform, $loca ...
- vue中监听路由参数变化
今天遇到一个这样的业务场景:在同一个路由下,只改变路由后面的参数值, 比如在这个页面 /aaa?id=1 ,在这个页面中点击一个按钮后 跳转到 /aaa?id=2 , 但从“/aaa?id=1”到“ ...
- vue 如何通过监听路由变化给父级路由菜单添加active样式
1.项目需求:在项目开发中,多级菜单的情况下,勾选子菜单时,需要在父级菜单添加active样式. 2.遇到的问题:一级路由菜单的话,点击当前路由会自动在路由标签上添加router-link-exact ...
随机推荐
- Protocol Buffer命名空间冲突
原文在这里. 什么是Protocol Buffer命名空间冲突? 所有链接到Go二进制文件的Protocol Buffer声明都被插入到一个全局注册表中. 每个Protocol Buffer声明(例如 ...
- 🛠 开源即时通讯(IM)项目OpenIM源码部署指南
OpenIM的部署涉及多个组件,并支持多种方式,包括源码.Docker和Kubernetes等.这要求在确保不同部署方式之间的兼容性同时,还需有效管理各版本之间的差异.确实,这些都是复杂的问题,涉及到 ...
- 根据pdf模板文件添加数据生成新的pdf与pdf添加读取二维码
参考文档 :https://www.cnblogs.com/ibeisha/p/itextsharp-pdf.html 程序demo 地址:https://github.com/hudean/itex ...
- 未能加载文件或程序集“System.ValueTuple, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。
一些老的项目在使用SAEA.Socket相关库后,程序本地测试正常,结果上传到服务器上后提示:未能加载文件或程序集"System.ValueTuple, Version=0.0.0.0, C ...
- 同时配置github和gitee秘钥
1.设置用户名和邮箱 git config --global --list 查看全局配置信息 git config --global --list 删除配置:必须删除该设置 git config -- ...
- ROS节点通信(三)action
官方wiki:http://wiki.ros.org/actionlib 目录 1.说明 2.代码示例 2.1.定义数据结构 2.1.1.goal 2.1.4.result 2.1.3.feedbac ...
- ajax中的同步异步和跨域请求
ajax中的同步异步和跨域请求 同步异步 demo.html <script> $.ajax({ type: "get", async: false, data: &q ...
- HP T520 改装DoraOS瘦客户机系统评测
HP T520 介绍 HP T520是一款瘦客户机产品.采用AMD GX-212JC 1.2 GHz 双核 SOC APU,带 AMD Radeon HD Graphics.配置4G 内存,8G SS ...
- 初步上手Git软件及GitHub平台:基本操作方法
本文介绍Git软件与GitHub平台的基本内容.使用方法与应用场景等. 目录 1 初步介绍 2 使用方法 2.1 GitHub配置 2.2 Git配置 2.3 代码上传至GitHub 1 初步介绍 ...
- 国产数据库TiDB初体验:简单易用,快速上手
最近开始关注国产数据库的发展,为了能从技术人员的角度来实际体验国产中目前最流行的TiDB数据库,从今天起,在官方公布的课程开始正面了解TiDB的设计理念. 看了2小时的入门课程介绍,总体来说,还是有不 ...