前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cssjshtml vue.js 全局守卫

cssjshtml vue.js 全局守卫

作者头像
葫芦
发布2019-04-17 15:43:10
1K0
发布2019-04-17 15:43:10
举报
文章被收录于专栏:葫芦葫芦

全局守卫: 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

代码语言:javascript
复制
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)
})
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019/01/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 全局守卫: 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
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档