12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import { createRouter, createWebHashHistory } from 'vue-router';
- import NProgress from 'nprogress'
- import NoteLayout from './layouts/NoteLayout.vue'
- import store from './store'
-
- const routes = [
- {
- path: '/',
- redirect: '/index',
- component: NoteLayout,
- children: [
- { path: 'index', name: 'index', component: () => import('./pages/index.vue') }
- ]
- },
- {
- path: '/login',
- name: 'login',
- component: () => import('./pages/login/index.vue')
- }
- ]
-
- const router = createRouter({
- // 4. 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
- history: createWebHashHistory(),
- routes, // `routes: routes` 的缩写
- })
-
- router.beforeEach((to, from, next) => {
- NProgress.start();
- const { user, getUserInfo } = store.getState('user')
- if (!user.token) {
- NProgress.done();
- if (to.name != 'login') {
- next({ name: 'login' })
- } else {
- next()
- }
- } else {
- if (!user.id) {
- getUserInfo(user.token).then(() => {
- NProgress.done();
- next()
- })
- } else {
- NProgress.done();
- next()
- }
- }
-
- })
-
- export default router;
|