search

Home  >  Q&A  >  body text

javascript - vue2.0 realizes that after logging in, all pages do not need to be logged in, and no login is required. The address of any page visited is transferred to the login page. How to achieve this?

vue After logging in, all pages do not need to be logged in, and no login is required. The address of any page accessed is transferred to the login page to implement the idea.
The project uses vue2.0 axios,

大家讲道理大家讲道理2779 days ago826

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-05-19 10:38:19

    Keywords:
    Token-based authentication, JWT, axios interceptor

    Link:
    Reference link

    reply
    0
  • 黄舟

    黄舟2017-05-19 10:38:19

    In the routing configuration, add the route that requires login

     meta: { requiresAuth: true }

    Determine whether the user is logged in in main.js, and jump to the login page if not logged in

    router.beforeEach((to, from, next) => {
        if (to.matched.some(record => record.meta.requiresAuth)) {
            //这里判断用户是否登录,我例子中是验证本地存储是否有token
            if (!localStorage.token) {
                next({
                    path: '/login',
                    query: { redirect: to.fullPath }
                })
            } else {
                next()
            }
        } else {
            next() // 确保一定要调用 next()
        }
    })

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-19 10:38:19

    Save the login status in local storage or cookie or vuex (preferably vuex), and then the rest is similar to the above

    reply
    0
  • Cancelreply