uniapp监听路由跳转

1,525 阅读1分钟

一、实现方法

  • 需要实现一个每次页面跳转的时候都能触发的事件,使用uni.addInterceptor实现,拦截路由跳转方法
  • 演示为全局监听,不同业务场景可进行不同操作,例如监听某个页面,具体使用以实际项目为准
	//在app.vue的onload生命周期,打开页面就开始监听
	onLaunch() {
			let that=this;
			uni.addInterceptor('navigateTo', {//监听跳转
				success(e) {
					that.watchRouter();
				}
			})
			uni.addInterceptor('redirectTo', {//监听关闭本页面跳转
				success(e) {
					that.watchRouter();
				}
			})
			uni.addInterceptor('switchTab', {//监听tabBar跳转
				success(e) {
					that.watchRouter();
				}
			})
			uni.addInterceptor('navigateBack', {//监听返回
				success(e) {
					that.watchRouter();
				}
			})
		}
	methods:{
		watchRouter(){
			console.log('路由跳转');
		}
	}