当前位置:网站首页>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>
边栏推荐
猜你喜欢

和AI一起玩儿剧本杀:居然比我还入戏

When does MySQL use table locks and when does it use row locks?

向量叉乘的几何意义及其模的计算

阿里二面:列出 Api 接口优化的几个技巧

@Bean 与 @Component 用在同一个类上,会怎样?

The CTO said I was not advised to use SELECT *, why is that?

首届人工智能安全大赛正式启动

Rodrigues: vector representation of rotation matrices

UDP和TCP使用同一个端口,可行吗?
Test Development Engineer Growth Diary 010 - CI/CD/CT in Jenkins (Continuous Integration Build/Continuous Delivery/Continuous Testing)
随机推荐
新人误删数据,组长巧用MySQL主从复制延迟挽回损失
Graphical relational database design ideas, this is too vivid
Station B collapsed, what would you do if you were the developer in charge that night?
你被MySQL 中的反斜杠 \\坑过吗?
云服务器零基础部署网站(保姆级教程)
debian problem
Ali Ermian: How many cluster solutions does Redis have?I answered 4
使用navicat连接mysql数据库时常报的错误:2003、1698、1251
schur completement
分布式系统中的开创者—莱斯利·兰伯特
The Geometric Meaning of Vector Cross Product and the Calculation of Modulus
window.open()的用法,js打开新窗体
阿里一面:多线程顺序运行有多少种方法?
Linx common directory & file management commands & VI editor usage introduction
go : 使用gorm创建数据库记录
bean的生命周期
Test Development Engineer Growth Diary 010 - CI/CD/CT in Jenkins (Continuous Integration Build/Continuous Delivery/Continuous Testing)
The first artificial intelligence safety competition officially launched
Rodrigues: vector representation of rotation matrices
Mobile phone side scroll to page to specify location