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

66 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-05-06 20:45:18 +08:00
import Vue from "vue";
import febAlive from "feb-alive";
2020-05-06 20:45:18 +08:00
import VueRouter from "vue-router";
2020-07-05 01:12:24 +08:00
import NProgress from "nprogress";
import "nprogress/nprogress.css";
2020-05-02 01:25:28 +08:00
// 路由数据
2020-05-06 20:45:18 +08:00
import routes from "./routes";
2020-07-05 01:12:24 +08:00
import store from "@/store/index";
2020-05-02 01:25:28 +08:00
// 在router实例化之前重写history
febAlive.resetHistory();
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) {
if (savePosition) {
return savePosition;
} else {
return {
x: 0,
y: 0,
};
}
},
2020-05-06 20:45:18 +08:00
routes,
});
2020-05-02 01:25:28 +08:00
Vue.use(febAlive, { router });
2020-05-02 01:25:28 +08:00
/**
* 路由拦截
* 权限验证
*/
router.beforeEach(async (to, from, next) => {
2020-07-05 01:12:24 +08:00
NProgress.start();
2020-05-13 05:19:42 +08:00
if (process.env.NODE_ENV === "development") {
console.log("before:");
console.log(to, from);
}
2020-07-05 01:12:24 +08:00
store.dispatch("acrou/cancelToken/cancel");
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) => {
2020-07-05 01:12:24 +08:00
NProgress.done();
2020-05-13 05:19:42 +08:00
if (process.env.NODE_ENV === "development") {
console.log("after:");
console.log(to);
}
2020-05-06 20:45:18 +08:00
});
2020-05-02 01:25:28 +08:00
2020-05-06 20:45:18 +08:00
export default router;