How to use vue to verify the existence of token in routing
This article mainly introduces "how to use vue to verify the existence of token in routing". In daily operation, I believe many people have doubts about how to use vue to verify the existence of token in routing. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to use vue to verify the existence of token in routing". Next, please follow the editor to study!
1. Verify in router/index.js
two。 The code is as follows:
Import Vue from 'vue'import Router from' vue-router'import Login from'@ / components/common/Login' .Vue.use (Router) const router = new Router ({routes: [{path:'/', name: Login, component: Login,}, {path:'/ home', component: Home, meta: {requiresAuth: true / / pages requiring verification, in which case their child pages are also validated. }, children: [{path:'/ paChong', name: 'PaChong', component: PaChong}]}) router.beforeEach ((to, from, next) = > {if (to.matched.some (record = > record.meta.requiresAuth)) {/ / which need to verify if (! sessionStorage.getItem ("token")) {/ / token existence condition next ({path:' /') / / Page to be redirected if verification fails: query: {redirect: to.fullPath / / parameters to be passed}})} else {next ()}} else {next () / / make sure that next ()}} export default router is called here The study on "how to use vue to verify the existence of token in routing" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!