当前位置:   article > 正文

vue3+nodejs配置路由_vue3 nodejs

vue3 nodejs

我的vue3+nodejs仓库地址 (根据千峰教育教学视频来的)

https://gitee.com/gyub/nodejs.giticon-default.png?t=N7T8https://gitee.com/gyub/nodejs.git

/router/config.js  路由模块化 添加权限标记

把红色区域里面的模块放在这个文件夹里面,方便管理

 /views/MainBox.vue

  1. import Center from '@/views/center/Center';
  2. import Home from '@/views/home/Home';
  3. import NewsAdd from '@/views/news-manage/NewsAdd';
  4. import NewsList from '@/views/news-manage/NewsList';
  5. import NotFund from '@/views/notFound/NotFund';
  6. import ProductAdd from '@/views/product-manage/ProductAdd';
  7. import ProductList from '@/views/product-manage/ProductList';
  8. import UserAdd from '@/views/user-manage/UserAdd';
  9. import UserList from '@/views/user-manage/UserList';
  10. const routes = [{
  11. path: '/',
  12. redirect: '/index'
  13. }, {
  14. path: '/:pathMatch(.*)*',
  15. name: NotFund,
  16. component: NotFund
  17. }, {
  18. path: '/center',
  19. component: Center
  20. }, {
  21. path: '/index',
  22. component: Home
  23. }, {
  24. path: '/news-manage/newsadd',
  25. component: NewsAdd
  26. }, {
  27. path: '/news-manage/newslist',
  28. component: NewsList
  29. }, {
  30. path: '/product-manage/productadd',
  31. component: ProductAdd
  32. }, {
  33. path: '/product-manage/productlist',
  34. component: ProductList
  35. }, {
  36. path: '/user-manage/useradd',
  37. component: UserAdd,
  38. requireAdmin: true
  39. }, {
  40. path: '/user-manage/userlist',
  41. component: UserList,
  42. requireAdmin: true
  43. }
  44. ]
  45. export default routes

/store/index.js  是否是第一次动态添加路由 侧边栏收缩 当前用户信息存储

  1. import { createStore } from 'vuex'
  2. import createPersistedState from 'vuex-persistedstate'
  3. export default createStore({
  4. state: {
  5. // 动态添加路由是否是第一次进来
  6. isFirstEnter: false,
  7. // aside侧边栏收缩
  8. isCollapse: true,
  9. // 当前用户信息存储
  10. userInfo: {}
  11. },
  12. getters: {
  13. },
  14. mutations: {
  15. changeFirstEnter(state, value) {
  16. state.isFirstEnter = value;
  17. },
  18. changeCollapse(state) {
  19. state.isCollapse = !state.isCollapse;
  20. },
  21. changeUserInfo(state, value) {
  22. state.userInfo = {
  23. ...state.userInfo,
  24. ...value
  25. }
  26. },
  27. // 退出登录时 清空当前用户信息
  28. clearUserInfo(state, value) {
  29. state.userInfo = {}
  30. }
  31. },
  32. actions: {
  33. },
  34. modules: {
  35. },
  36. // 持久化
  37. plugins: [createPersistedState({
  38. // 控制是否持久化
  39. paths: ["isCollapse", "userInfo"]
  40. })]
  41. })

/router/index.js  router.addRoute 路由守卫 动态添加路由 添加权限设置

  1. import { createRouter, createWebHashHistory } from 'vue-router';
  2. import Login from '@/views/Login.vue';
  3. import MainBox from '@/views/MainBox.vue';
  4. import routerConfig from '@/router/config';
  5. import store from '@/store/index';
  6. const routes = [
  7. {
  8. path: '/login',
  9. name: 'login',
  10. component: Login
  11. },
  12. {
  13. path: '/mainbox',
  14. name: 'mainbox',
  15. component: MainBox
  16. }
  17. ]
  18. const router = createRouter({
  19. history: createWebHashHistory(),
  20. routes
  21. })
  22. router.beforeEach((to, from, next) => {
  23. if (to.name === 'login') {
  24. next();
  25. return
  26. } else {
  27. if (!localStorage.getItem('token')) {
  28. next({ path: '/login' })
  29. } else {
  30. // 动态路由走一遍就好
  31. if (!store.state.isFirstEnter) {
  32. // 防止动态路由由于权限原因重复添加----删除嵌套路由
  33. router.removeRoute('mainbox');
  34. // 添加嵌套路由
  35. configRouter();
  36. next({ path: to.path });
  37. } else {
  38. next();
  39. }
  40. }
  41. }
  42. })
  43. // 动态添加嵌套路由
  44. const configRouter = () => {
  45. // 判断路由里面是否已存在mainbox
  46. if(!router.hasRoute('mainbox')) {
  47. router.addRoute({
  48. path: '/mainbox',
  49. name: 'mainbox',
  50. component: MainBox
  51. })
  52. }
  53. routerConfig.forEach(item => {
  54. checkPermission(item) && router.addRoute('mainbox', item)
  55. })
  56. // 第一次动态添加嵌套路由
  57. store.commit('changeFirstEnter', true);
  58. }
  59. // 判断当前用户是否有管理员权限
  60. const checkPermission = (item) => {
  61. // 在路由配置做的权限标记
  62. if(item.requireAdmin) {
  63. return store.state.userInfo.role==1
  64. }
  65. return true
  66. }
  67. export default router;

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号