uniapp 监听路由跳转

需要实现一个监听页面每次跳转的时候都能触发的事件,使用 uni.addInterceptor uni拦截器实现,拦截路由跳转方法

// APP.vue文件
<script>
export default {
	onLaunch: function () {
		let that = this,
			naviArr = [
				'navigateTo',
				'redirectTo',
				'reLaunch',
				'switchTab',
				'navigateBack',
			]
		for (let i of naviArr) {
			uni.addInterceptor(i, {
				//监听跳转
				success(e) {
					that.watchRouter()
				},
			})
		}
		that.watchRouter()
		//console.log('App Launch')
	},
	onShow: function () {
		//console.log('App Show')
	},
	onHide: function () {
		//console.log('App Hide')
	},
	methods: {
		watchRouter() {
			console.log('监听uniapp页面路由跳转')
		},
	},
}
</script>

<style lang="scss">
/*每个页面公共css */
</style>

监听移动端的物理返回键

注意:微信小程序不支持。

methods: {
},
onBackPress(options) {
  console.log("用户按下物理返回键");
},

猜你喜欢

转载自blog.csdn.net/weixin_43743175/article/details/127313441
今日推荐