vue 路由 重定向

1、main.js 判断跳转

import Vue from 'vue'
import App from './App'
import router from './router/common'
import Dialog from 'hsy-vue-dialog'
import store from './store/'
import './config/style'
import HappyScroll from 'vue-happy-scroll'
import 'vue-happy-scroll/docs/happy-scroll.css'
import 'babel-polyfill'
import './common/js/common.js'
import ECharts from 'vue-echarts'
Vue.component('chart', ECharts)
const isDebug_mode = process.env.NODE_ENV !== 'production'
Vue.config.debug = isDebug_mode
Vue.config.devtools = isDebug_mode
Vue.config.productionTip = isDebug_mode

Vue.use(HappyScroll)
Vue.use(Dialog)

router.beforeEach((to, from, next) => {
  // 没登录则跳转到登录界面
  if (
    !sessionStorage.getItem('auid') &&
    (to.path !== '/login' &&
      to.path !== '/loginSite')
  ) {
    next({
      path: '/login'
    })
    // debugger  判断是否登录
  } else {
    next()
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '',
  components: {
    App
  }
})

2、router.js 路由

import Vue from 'vue'
import Router from 'vue-router'
import dataCenter from './data_center' // 数据中心
import subject from './subject/index.js' // 一张图
import pipe from './pipe/index.js' // 一张图
import screen from './screen/index.js' // 大屏
import desk from './desk/index.js' // 大屏
import businessManage from './business_management' // 业务管理
import statisticalAnalysis from './statistical_analysis' // 统计分析
import prevention from './prevention' // 统计分析

/**
 * 登录
 * @param  {[type]} r [description]
 * @return {[type]}   [description]
 */
Vue.use(Router)

export default new Router({
  // mode: 'history',
  routes: [{
    path: '/',
    redirect() {
      return '/login'
    }
  },
  {
    path: '/login',
    name: '登录',
    component: () => import('@/views/common/login.vue')
  },
  //  重定向的路由  相当于 /loginSite
  {
    path: '/welcome',
    redirect() {
      return '/loginSite'
    }
  },
  {
    path: '/loginSite',
    component: () => import('@/views/common/loginSite.vue')
  },
  ...subject,
  ...screen,
  ...desk,
  ...dataCenter,
  ...businessManage,
  ...statisticalAnalysis,
  ...pipe,
  ...prevention
  ],

  // keep-alive 滚动条
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      if (from.meta.keepAlive) {
        from.meta.savedPosition = document.body.scrollTop
      }
      return {
        x: 0,
        y: to.meta.savedPosition || 0
      }
    }
  }
})

你可能感兴趣的:(vue 路由 重定向)