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 ...
随机推荐
- [LeetCode刷题记录]39 组合总和
题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的数字可以无限制 ...
- SqlSugar Code First
注意点 1.SqlSugar Code First可以快速开发,使用起来也要分阶段使用,比如早期随便搞,中后期需要禁用一些功能保证数据安全(标题6和7 ) 2.数据库账号需要有比较高的权限, 3. ...
- Markdown-CSDN自带帮助语法
这里写自定义目录标题 欢迎使用Markdown编辑器 新的改变 功能快捷键 合理的创建标题,有助于目录的生成 如何改变文本的样式 插入链接与图片 如何插入一段漂亮的代码片 生成一个适合你的列表 创建一 ...
- C++ Qt开发:运用QJSON模块解析数据
Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍如何运用QJs ...
- Vue核心概念与其指令
Vue简述 Vue是一套构建用户UI界面的前端框架. 构建用户界面的意思是:往html中填充数据,框架的意思是:一套开发规范. Vue的特点 1.数据驱动视图 当页面是一个普通的数据展示时,数据改 ...
- InnoDB存储引擎的行级锁
InnoDB存储引擎的行级锁 InnoDB存储引擎和MyISAM的其中有两个很重要的区别:一个是事务,一个就是锁机制不同.事务之前有介绍,有问题的去补课;锁方面的不同是InnoDB引擎既有表锁又有行锁 ...
- CF940F Machine Learning题解
题目链接:洛谷 或者 CF 不是特别难的题,抽象下题意就是算区间次数出现的次数 mex 和带单点修改.看到范围 \(1e5\) 还带修改,传统的 mex 求法里貌似就莫队类算法好带修,考虑带修莫队. ...
- 教你用JavaScript实现表情评级
案例介绍 欢迎来到我的小院,我是霍大侠,恭喜你今天又要进步一点点了!我们来用JavaScript编程实战案例,做一个表情评价程序.用户打星进行评价,表情会根据具体星星数量发生变化. 案例演示 点击星星 ...
- Excel如何核对同一行的两列数据是否一致
方法一 Ctrl+G 快捷键Ctrl+G,点击[定位条件],选择"行内容差异单元格",点击[确定]. 方法二 条件格式 逆向思维,先利用条件格式查找出相同的数据,筛选剔除相同的数据 ...
- windows-网页流视频下载
一.m3u8 视频在线提取工具 https://blog.luckly-mjw.cn/tool-show/m3u8-downloader/index.html