66 导航守卫

我们来看看其他的模块,这个项目我们已经做得差不多了,然后订单管理我们就不做了,因为订单管理2,3状态正常渲染就可以了,接下来我们来看一下,拦截的问题,vue的导航守卫-拦截,具体如何操作呢?比如说,我们已经登录状态,我可以进入到地址管理,虽然我没有地址,但是我可以进去,如果说我是一个未登录状态,我点击地址管理,他会报一个错误,我们该如何处理?
首先,他会跳转到'/login',原因是我们在API里面有封装,如果未登录,他就会跳转到'/login',他此时是已经进去了,发现app.user没有,才跳转到登录页面,其实我们是不能让他进去的,所以此时我们要做导航拦截。
解决报错到 src/router/index.js 文件中添加如下代码
复制代码
const originalPush = Router.prototype.push
const originalReplace = Router.prototype.replace

// push
Router.prototype.push = function push (location,onResolve,onReject) {
	if (onResolve || onReject) return originalPush.call(this,location, onResolve,onReject)
	return originalPush.call(this,location).catch(err => err)
}

// replace
Router.prototype.replace = function push(location,onResolve,onReject) {
	if (onResolve || onReject) return originalReplace.call(this,location,onResolve,onReject)
	return originalReplace.call(this,location).catch(err => err)
}


自己的
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace

// push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
	return originalPush.call(this, location).catch(err => err)
}

// replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
	return originalReplace.call(this, location).catch(err => err)
}
导航守卫 实现代码如下
复制代码
1, src/router/index.js

import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace

// push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
	return originalPush.call(this, location).catch(err => err)
}

// replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
	return originalReplace.call(this, location).catch(err => err)
}

Vue.use(VueRouter);


const routes = [{
		path: "/home",
		name: "Home",
		component: Home,
	},
	{
		path: "/",
		redirect: "/home", // 重定向
	}, {
		path: "/list",
		name: "List",
		component: () =>
			import("../views/List.vue"),
	}, {
		path: "/cart",
		name: "Cart",
		meta: {
			keepAlive: true, // 此组件需要被缓存
		},
		component: () =>
			import("../views/Cart.vue"),
	}, {
		path: "/my",
		name: "My",
		component: () =>
			import("../views/My.vue"),
	},
	{
		path: "/search",
		children: [{
			path: "/",
			name: "index",
			component: () =>
				import("../views/search/Search-index.vue"),
		}, {
			path: "/search/list",
			name: "Search-list",
			component: () =>
				import("../views/search/Search-list.vue"),
		}],
		component: () =>
			import("../views/Search.vue"),
	},
	{
		path: "/detail",
		name: "Detail",
		meta: {
			keepAlive: true, // 此组件需要被缓存
		},
		component: () =>
			import("../views/Detail.vue"),
	},
	{
		path: "/login",
		name: "Login",
		component: () =>
			import("../views/login/Login.vue"),
	},
	{
		path: "/userLogin",
		name: "UserLogin",
		component: () =>
			import("../views/login/UserLogin.vue"),
	},
	{
		path: "/register",
		name: "Register",
		component: () =>
			import("../views/login/Register.vue"),
	},
	{
		path: "/recovery",
		children: [{
			path: "/",
			name: "RecoveryIndex",
			component: () =>
				import("../views/recovery/RecoveryIndex.vue"),
		}, {
			path: "/recovery/btn",
			name: "RecoveryBtn",
			component: () =>
				import("../views/recovery/RecoveryBtn.vue"),
		}],
		component: () =>
			import("../views/recovery/Recovery.vue"),
	},
	{
		path: "/path",
		children: [{
			path: "/",
			name: "Path-Index",
			component: () =>
				import("../views/path/Path-Index.vue"),
		}, {
			path: "/path/path-list",
			name: "Path-List",
			component: () =>
				import("../views/path/Path-List.vue"),
		}, {
			path: "/path/path-province-city",
			name: "Path-Province-City",
			component: () =>
				import("../views/path/Path-Province-City_bak.vue"),
		}, {
			path: "/path/path-address",
			name: "Path-Address",
			component: () =>
				import("../views/path/Path-Address_bak.vue"),
		}],
		component: () =>
			import("../views/Path.vue"),
	},
	{
		path: "/order",
		name: "Order",
		meta: {
			keepAlive: true, // 此组件需要被缓存
		},
		component: () =>
			import("../views/Order.vue"),
	},
	{
		path: "/payment",
		name: "Payment",
		component: () =>
			import("../views/Payment.vue"),
	}
];

const router = new VueRouter({
	mode: "history",
	base: process.env.BASE_URL,
	routes,
});

// 拦截需要验证页面必须为登录状态才可以进入
router beforeEach((to, from, next) => {
	// 哪些页面需要验证在此加入即可
	let nextRoute = ['Payment', 'Cart', 'Path', 'Order', 'Path-Index', 'Path-List'];

	// 判断 是否为登录中
	let userInfo = JSON.parse(localStorage.getItem('teaUserInfo'));

	// 当前进入的页面,是不是需要验证的哪些页面
	if (nextRoute.indexOf(to.name) >= 0) {
		// 验证是否为登录状态
		if (!userInfo) {
			// 跳转到登录页面
			router.push('/login');
		}
	}

	next();

	// console.log(to, from, next);
})

export default router;
相关推荐
小徐_2333几秒前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
山河木马1 小时前
矩阵专题3-怎么创建投影矩阵(uProjectionMatrix)
javascript·webgl·计算机图形学
天蓝色的鱼鱼2 小时前
关于 CSS 你可能不知道的属性,但关键时刻很有用
前端·css
泯泷3 小时前
第 2 篇:设计第一套字节码:Opcode、Instruction 与 Constant Pool
前端·javascript·安全
妙码生花3 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十五):优化细节、网络请求封装
前端·后端·ai编程
泯泷3 小时前
第 1 篇:从 1 + 2 开始:亲手写出第一台 JSVM
前端·javascript·安全
团团崽_七分甜3 小时前
Spring Boot 核心知识点总结
前端
lichenyang4533 小时前
从一个按钮开始,理解 ASCF 框架到底在做什么
前端
古夕4 小时前
第三方 SSO 接入实践:redirect_uri 编码、回调一致性与跨项目联调
前端·vue.js