uniapp 小程序 h5 路由拦截 监听路由

1,860 阅读1分钟

目前就想到了这个方法 各位大佬 有什么好的方法可以写在评论区

//App.vue
<script>
export default {
	onLaunch: function() {
		console.log('App Launch');
		this.getConfig();
		let that = this;
		//#ifdef MP-WEIXIN
		wx.onAppRoute(page => {
			this.watchRouter(page.path);
		});
		//#endif
	
	},
	//#ifdef H5
	watch: {
		$route: {
			immediate: true,
			deep:true,
			handler(newVal, oldVal) {
				this.watchRouter(newVal.meta.pagePath)
				//该函数会执行
			}
		}
	},
	//#endif 
	methods: {
		watchRouter(route) {  
			let Authorization = uni.getStorageSync('Authorization');
			if (route !== 'pages/index/startPage/startPage' && !Authorization) {
				console.log('重定向')
				this.$nextTick(function(){
					uni.redirectTo({
						url: '/pages/index/startPage/startPage',
						fail(e) {}
					});
				})
			}
		}, 
	}
};
</script>

chatgpt对话小程序 ai对话ai画图 扫码体验

gh_fee1287c2853_258.jpg