cssjshtml vue.js 全局守卫

葫芦的运维日志

下一篇 搜索 上一篇

2019/01/09 16:46


全局守卫:
router.beforeEach((to,from,next)=>{
  //判断 store.gettes.isLogin===false
  if(to.path=='/login' || to.path=='/register'){
    next();
  }else{
    alert("还没有登录,请先登录");
    next('/login');

  }})

如果去的不是login或者register 弹出提示窗。

main.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
import Home from './components/Home'
import Menu from './components/Menu'
import Admin from './components/Admin'
import About from './components/about/About'
import Login from './components/Login'
import Register from './components/Register'

//二级路由
import Contact from './components/about/Contact'
import Delivery from './components/about/Delivery'
import History from './components/about/History'
import OderingGuide from './components/about/OderingGuide'

// 三级路由
import Person from './components/about/contact/PersonName'
import PhoneNumber from './components/about/contact/Phone'

Vue.use(VueRouter)

const routes = [
  {path: '/', component: Home},
  {path: '/menu', name: "menulink", component: Menu},
  {path: '/admin', name: "adminlink", component: Admin},
  {
    path: '/about', name: "aboutlink", redirect: '/about/contact', component: About, children: [
      {
        path: '/about/contact', name: "contactLink", redirect: '/personname', component: Contact, children: [
          {path: '/phone', name: "phoneNumber", component: PhoneNumber},
          {path: '/personname', name: "personName", component: Person}

        ]
      },
      {path: '/history', name: "historyLink", component: History},
      {path: '/delivery', name: "deliveryLink", component: Delivery},
      {path: '/oderingguide', name: "oderingGuideLink", component: OderingGuide},
    ]
  },
  {path: '/login', name: "loginlink", component: Login},
  {path: '/register', name: "registerlink", component: Register},
  {path: "*", redirect: '/'}
]
const router = new VueRouter({
  routes,
  mode: 'history'

})

router.beforeEach((to,from,next)=>{
  //判断 store.gettes.isLogin===false
  if(to.path=='/login' || to.path=='/register'){
    next();
  }else{
    alert("还没有登录,请先登录");
    next('/login');

  }

})


new Vue({
  el: '#app',
  router,
  render: h => h(App)
})

葫芦的运维日志

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2019 王梓 赞助联系方式 陕ICP备17005322号