goindex-theme-r/src/router/index.js

43 lines
949 B
JavaScript
Raw Normal View History

2020-05-06 20:45:18 +08:00
import Vue from "vue";
import VueRouter from "vue-router";
2020-05-02 01:25:28 +08:00
// 路由数据
2020-05-06 20:45:18 +08:00
import routes from "./routes";
2020-05-02 01:25:28 +08:00
// fix vue-router NavigationDuplicated
2020-05-06 20:45:18 +08:00
const VueRouterPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
return VueRouterPush.call(this, location).catch((err) => err);
};
const VueRouterReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
return VueRouterReplace.call(this, location).catch((err) => err);
};
2020-05-02 01:25:28 +08:00
2020-05-06 20:45:18 +08:00
Vue.use(VueRouter);
2020-05-02 01:25:28 +08:00
// 导出路由 在 main.js 里使用
const router = new VueRouter({
2020-05-06 20:45:18 +08:00
mode: "history",
scrollBehavior(to, from, savePosition) {
return { x: 0, y: 0 };
},
routes,
});
2020-05-02 01:25:28 +08:00
/**
* 路由拦截
* 权限验证
*/
router.beforeEach(async (to, from, next) => {
2020-05-06 20:45:18 +08:00
console.log("before:");
2020-05-07 14:54:04 +08:00
console.log(to,from);
2020-05-06 20:45:18 +08:00
next();
});
2020-05-02 01:25:28 +08:00
2020-05-06 20:45:18 +08:00
router.afterEach((to) => {
console.log("after:");
console.log(to);
});
2020-05-02 01:25:28 +08:00
2020-05-06 20:45:18 +08:00
export default router;