main.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue'
  4. import App from './App'
  5. import router from './router'
  6. import store from './store'
  7. import ElementUI from 'element-ui'
  8. import 'element-ui/lib/theme-default/index.css'
  9. import NProgress from 'nprogress'
  10. import 'normalize.css/normalize.css'
  11. import '@/assets/iconfont/iconfont'
  12. import IconSvg from '@/components/Icon-svg/index.vue'
  13. Vue.config.productionTip = false
  14. Vue.use(ElementUI);
  15. Vue.component('icon-svg', IconSvg)
  16. const whiteList = ['/login'];
  17. router.beforeEach((to, from, next) => {
  18. NProgress.start();
  19. if (store.getters.token) {
  20. if (to.path === '/login') {
  21. next({ path: '/' });
  22. } else {
  23. if (store.getters.roles.length === 0) {
  24. store.dispatch('GetInfo').then(res => {
  25. const roles = res.data.role;
  26. store.dispatch('GenerateRoutes', { roles }).then(() => {
  27. router.addRoutes(store.getters.addRouters)
  28. next(Object.assign({}, to));
  29. })
  30. })
  31. } else {
  32. next();
  33. }
  34. }
  35. } else {
  36. if (whiteList.indexOf(to.path) !== -1) {
  37. next()
  38. } else {
  39. next('/login');
  40. NProgress.done();
  41. }
  42. }
  43. });
  44. router.afterEach(() => {
  45. NProgress.done();
  46. });
  47. new Vue({
  48. el: '#app',
  49. router,
  50. store,
  51. template: '<App/>',
  52. components: { App }
  53. })