Giter VIP home page Giter VIP logo

Comments (8)

woai3c avatar woai3c commented on July 29, 2024

permission.js 的代码发一下

from vue-admin-template.

xwhuxx avatar xwhuxx commented on July 29, 2024

这部分我应该没怎么改动
import { LoadingBar } from 'view-design'
import createRoutes from '@/utils/createRoutes'
import router from './router'
import store from './store'
import { getDocumentTitle, resetTokenAndClearUser } from './utils'

// 是否有菜单数据
let hasMenus = false
router.beforeEach(async (to, from, next) => {
document.title = getDocumentTitle(to.meta.title)
LoadingBar.start()
if (localStorage.getItem('token')) {
if (to.path === '/login') {
next({ path: '/' })
} else if (hasMenus) {
next()
} else {
try {
const routes = createRoutes(store.state.menuItems)
router.addRoutes(routes)
hasMenus = true
next({ path: to.path || '/' })
} catch (error) {
resetTokenAndClearUser()
next(/login?redirect=${to.path})
}
}
} else {
hasMenus = false
if (to.path === '/login') {
next()
} else {
next(/login?redirect=${to.path})
}
}
})

router.afterEach(() => {
LoadingBar.finish()
})

from vue-admin-template.

woai3c avatar woai3c commented on July 29, 2024

发代码的时候把代码放在两个 ``` 中间。

```
test
```

类似这样

from vue-admin-template.

xwhuxx avatar xwhuxx commented on July 29, 2024

不好意思,比较少提issue,不大清楚,我比较了下你这边的,应该只是我把import createroute提前了,因为我这边如果不提前他就会报错

import { LoadingBar } from 'view-design'
import createRoutes from '@/utils/createRoutes'
import router from './router'
import store from './store'
import { getDocumentTitle, resetTokenAndClearUser } from './utils'

// 是否有菜单数据
let hasMenus = false
router.beforeEach(async (to, from, next) => {
document.title = getDocumentTitle(to.meta.title)
LoadingBar.start()
if (localStorage.getItem('token')) {
if (to.path === '/login') {
next({ path: '/' })
} else if (hasMenus) {
next()
} else {
try {
const routes = createRoutes(store.state.menuItems)
router.addRoutes(routes)
hasMenus = true
next({ path: to.path || '/' })
} catch (error) {
resetTokenAndClearUser()
next(/login?redirect=${to.path})
}
}
} else {
hasMenus = false
if (to.path === '/login') {
next()
} else {
next(/login?redirect=${to.path})
}
}
})

router.afterEach(() => {
LoadingBar.finish()
})

from vue-admin-template.

woai3c avatar woai3c commented on July 29, 2024

这个看着没问题,看一下路由文件,估计是重定向导致的。

from vue-admin-template.

xwhuxx avatar xwhuxx commented on July 29, 2024
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const commonRoutes = [
    {
        path: '/login',
        name: 'login',
        meta: { title: '登录' },
        component: () => import('../components/Login.vue'),
    },
    {
        path: '/404',
        name: '404',
        meta: { title: '404' },
        component: () => import('../components/404.vue'),
    },
    { path: '/', redirect: '/home' },
]

// 本地所有的页面 需要配合后台返回的数据生成页面
export const asyncRoutes = {
    home: {
        path: '/home',
        name: 'home',
        meta: { title: '主页' },
        component: () => import('../views/Home.vue'),
    },
    messagelist: {
        path: '/messagelist',
        name: 'messagelist',
        meta: { title: '信息展示' },
        component: () => import('../views/messagelist.vue'),
    },
}

const createRouter = () => new Router({
    routes: commonRoutes,
})

const router = createRouter()

export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher
}

export default router

from vue-admin-template.

xwhuxx avatar xwhuxx commented on July 29, 2024

补充下store信息

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        // 左侧菜单栏数据
        menuItems: [
            {
                name: 'home',
                text: '主页',
                size: 18,
                type: 'md-home',
            },
            {
                size: 18,
                text: '主机信息',
                type: 'md-desktop',
                children: [
                    {
                        size: 18,
                        type: 'ios-grid',
                        name: 'messagelist',
                        text: '信息展示',
                    },
                ],
            },
        ],
    },
    mutations: {
        setMenus(state, items) {
            state.menuItems = [...items]
        },
    },
})

from vue-admin-template.

woai3c avatar woai3c commented on July 29, 2024
{ path: '/', redirect: '/home' },

这行代码有问题,把它放到异步路由里。

from vue-admin-template.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.