当前位置:网站首页>Vue2进阶篇-编程式路由导航、缓存路由组件、路由的激活与失活
Vue2进阶篇-编程式路由导航、缓存路由组件、路由的激活与失活
2022-07-30 05:51:00 【大脑不能为空】
Vue2基础全套教程合集:点击跳转 Vue2高级全套教程合集:点击跳转
一、编程式路由导航
作用:不借助
<router-link>
实现路由跳转,让路由跳转更加灵活使用场景:需要点击不是router-link标签定义的按钮进行跳转时,可以使用该方法。
具体编码:
//$router的两个API this.$router.push({ name:'xiangqing', params:{ id:xxx, title:xxx } }) this.$router.replace({ name:'xiangqing', params:{ id:xxx, title:xxx } }) this.$router.forward() //前进 this.$router.back() //后退 this.$router.go() //可前进也可后退
1. 编程式路由导航多次点击报错问题
- 原因:[email protected] 之后添加了同一路径跳转错误异常功能导致。
- 解决方法:重写
$router.push()
和$router.replace()
方法
// 重写push|replace
// function(跳转路径,请求成功回调函数,请求失败回调函数)
// call|apply区别
// 相同点:都可以调用函数一次,都可以篡改函数上下文一次。
// 不同点:call传参用逗号隔开,apply传参用数组传递。
VueRouter.prototype.push = function (location, resolve, reject) {
if (resolve && reject) {
//代表着两个形参接受参数【箭头函数】
originPush.call(this, location, resolve, reject);
} else {
originPush.call(this, location, () => {
}, () => {
});
}
};
VueRouter.prototype.replace = function (location, resolve, reject) {
if (resolve && reject) {
//代表着两个形参接受参数【箭头函数】
originReplace.call(this, location, resolve, reject);
} else {
originReplace.call(this, location, () => {
}, () => {
});
}
}
二、缓存路由组件
作用: 让不展示的路由组件保持挂载,不被销毁。
使用场景: 组件中含有输入框,在切换组件时不希望输入框内容清空,可以使用keep-alive缓存。
具体编码:
<keep-alive include="News"> <router-view></router-view> </keep-alive>
三、路由的激活与失活
- 作用: 路由组件所独有的两个钩子,用于捕获路由组件的激活状态。
- 使用场景: 在组件中开启了定时器,可以在组件失活进行关闭,防止资源浪费。
- 具体名字:
activated
路由组件被激活时触发。deactivated
路由组件失活时触发。
<script> export default { name: 'News', data() { return { opacity: 1 } }, activated() { console.log('News组件被激活了') this.timer = setInterval(() => { console.log('@') this.opacity -= 0.01 if (this.opacity <= 0) this.opacity = 1 }, 16) }, deactivated() { console.log('News组件失活了') clearInterval(this.timer) }, } </script>
边栏推荐
- Test Development Engineer Growth Diary 010 - CI/CD/CT in Jenkins (Continuous Integration Build/Continuous Delivery/Continuous Testing)
- Linx常见目录&文件管理命令&VI编辑器使用 介绍
- export , export default,import完整用法
- 《心智社会》—马文·明斯基
- go : go-redis 基础操作
- LVM and disk quotas
- Playing script killing with AI: actually more involved than me
- The calculation of the determinant of the matrix and its source code
- 空间顶点到平面的距离计算的证明及其源码
- Boot process and service control
猜你喜欢
人工肌肉智能材料新突破
Test Development Engineer Growth Diary 010 - CI/CD/CT in Jenkins (Continuous Integration Build/Continuous Delivery/Continuous Testing)
C#的访问修饰符,声明修饰符,关键字有哪些?扫盲篇
向量三重积的等式推导证明
MYSQL下载及安装完整教程
相机坐标系,世界坐标系,像素坐标系三者转换,以及OPENGLDEFocal Length和Opengl 的 Fov转换
AI can identify race from X-rays, but no one knows why
Detailed explanation of numpy multidimensional array ndarray
Go 使用mencached缓存
Boot process and service control
随机推荐
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
go : 使用gorm修改数据
The usage of window.open(), js opens a new form
The calculation proof of the intersection of the space line and the plane and its source code
Derivative Operations on Vectors and Derivative Operations on Vector Cross and Dot Products
go : go-redis set操作
大飞机C919都用了哪些新材料?
DHCP principle and configuration
云服务器零基础部署网站(保姆级教程)
分布式系统中的开创者—莱斯利·兰伯特
向量叉乘的几何意义及其模的计算
Station B collapsed, what would you do if you were the developer in charge that night?
What happens when @Bean and @Component are used on the same class?
空间顶点到直线的距离计算及其源码
Rodrigues: vector representation of rotation matrices
Go 使用mencached缓存
UDP和TCP使用同一个端口,可行吗?
Required request body is missing 问题解决
Go语学习笔记 - gorm使用 - 数据库配置、表新增 Web框架Gin(七)
New breakthrough in artificial muscle smart materials