1. 程式人生 > >Vue——路由定義及基本使用

Vue——路由定義及基本使用

Vue.use(VueRouter)

/**定義路由**/
const routes = [
  {
    path: '/',
    component: login
  },
  {
    path: '/login',
    name :'login',
    component: login
  },
  { path: '*', component: notfound },/*url路徑跟路由跳轉不匹配,404*/
  {
    path: '/table',
    component: table,
      meta: {
               requireAuth: true
, // 新增該欄位,表示進入這個路由是需要登入的 }, children: [ { path: '/', component: batteryFactory }, { path: 'batteryFactory', component: batteryFactory }, { path: 'batteryManage', component: batteryManage } ] } ]; // 頁面重新整理時,重新賦值token
if (window.sessionStorage.getItem('token')) { store.commit(types.LOGIN, window.sessionStorage.getItem('token')) } const router = new VueRouter({ mode: 'history', routes: routes }); router.beforeEach((to, from, next) => { debugger if (to.matched.some(r => r.meta.requireAuth)) {// 判斷該路由是否需要登入許可權
if (store.state.token) {// 通過vuex state獲取當前的token是否存在 next(); } else { next({ path: '/login', query: {redirect: to.fullPath} // 將跳轉的路由path作為引數,登入成功後跳轉到該路由 }) } } else { next(); } })