uni-app学习笔记之router路由跳转规则

//官方示例
//import router from "@/router/router.js";
//需要在main.js中导入才起效
///router/router.js
let needLogin = ["/pages/auth/resetPwd",
	"/pages/auth/updatePwd",
	"/pages/center/center",
	"/pages/detail/detail",
	"/pages/index/index",
	"/pages/index/add",
];
arr = ["navigateTo", "redirectTo", "switchTab", "reLaunch"];
arr.forEach(value => {
	uni.addInterceptor(value, {
		invoke(e) { // 调用前拦截
			//获取用户的token
			const token = uni.getStorageSync('uni_id_token')
			console.log(token);
			//获取当前页面路径(即url去掉"?"和"?"后的参数)
			const url = e.url.split('?')[0];
			console.log(url);
			//判断要打开的页面是否需要验证登录
			if (needLogin.includes(url) && token == '') {
				console.log("拦截成功", url);
				uni.reLaunch({
					url: "/pages/auth/login"
				})
				return false
			}
		},
		fail(err) { // 失败回调拦截 
			console.log(err);
		},
	})
})

你可能感兴趣的:(uni-app,javascript,前端,vue.js)