当前位置:   article > 正文

vue3 + vite4 + vue-router4 + pinia 入门级管理后台_vite4-admin

vite4-admin

演示地址已经失效:演示地址

更新时间[2023-03-03],所有安装包会不定时最新版更新,保证可运行性。不能更新的也会在文档中说明。

最新全家桶GitHub地址:vue3 + vite4 + vue-router4 + pinia

一下内容是vue3 + vue-cli配置的全家桶,不过不太建议看了。大家直接使用最新的GitHub项目实战即可,已经在公司项目中使用了。

零零碎碎搞了这个小东西,还有很多不完善的地方。希望各位大佬共同学习和进步,文章若有不对之处请多多指正。在各位阅读这篇文章的时候,就当各位有一定的基础了。否则请各位先了解下基础,以下是参考文章,完整项目在文章最后。

ps:最近突然发现,我的国际化vue-i18n使用方法有问题,虽然可以实现功能,但总感觉很怪异,目前正在抽时间解决,如果有哪位大佬知道如何解决的,可以留言,感谢。

vue-cli:Migrating from v3 | Vue CLI

vue:Vue3js(中文文档:Vue3中文文档 - vuejs

vue-router:Home | Vue Router

vuex:Vuex 是什么? | Vuex

正文

基础配置项目中都已经写了,大家如果有疑问或者不适合的地方可以自行修改完善。

  1. ├── package.json 包管理
  2. ├── public
  3. │   ├── favicon.ico
  4. │   ├── img
  5. │   ├── index.html
  6. │   └── robots.txt
  7. ├── src
  8. │   ├── App.vue
  9. │   ├── api 接口定义
  10. │   ├── assets 静态资源
  11. │   ├── components 组件
  12. │   ├── libs 工具库
  13. │   ├── main.js 启动入口
  14. │   ├── mock 数据mock
  15. │   ├── plugins 组件工具
  16. │   ├── registerServiceWorker.js
  17. │   ├── router 路由
  18. │   ├── store vuex
  19. │   └── views 页面
  20. └── vue.config.js 启动配置

以上是大概的目录对应,好了开始正式撸代码了,各位坐好各自的板凳:

和vue2相同的配置在这不再赘述

1、vue.config.js:

总体来说和vue2没有多大区别,修改的地方不多主要就是 chainWebpack、pluginOptions较vue2配置有些不同,不过问题不大,大家根据自己的要求按照官方给出的格式配置就行,这个是我配置指定路径别名的:

  1. config.resolve.alias
  2. .set("@", resolve("src"))
  3. .set("@api", resolve("src/api"))
  4. .set("@assets", resolve("src/assets"))
  5. .set("@components", resolve("src/components"))
  6. .set("@libs", resolve("src/libs"))
  7. .set("@plugins", resolve("src/plugins"));

2、main.js:

vue3变化很大,各种使用方式都改了,大家可以参考官方文档进行配置,我在这里并没有配置太多东西,毕竟以学习为主:

  1. import { createApp } from "vue";
  2. import App from "./App.vue";
  3. const app = createApp(App);
  4. // 全局变量配置方式
  5. import filters from "@plugins/filters";
  6. app.config.globalProperties.$filters = filters;
  7. // 链式使用,个人感觉非常舒服
  8. app.use(router).use(store).mount("#app");

3、*store(vuex):

这里主要处理获取后端的菜单数据等等,组装成路由,其中vue-router对*处理方式修改为了正则匹配的方式 /:pathMatch(.*)* & /:pathMatch(.*),原来的*在router中会undefined,核心方法

  1. // 组装动态路由
  2. const setRouter = (dataList) => {
  3. // 必须为根路由,不能在其他地方生成component,除非在store里面
  4. // () => import("@/views/Index"),
  5. // () => Promise.resolve(require(`@/views/${view}`).default)
  6. // (resolve) => require([`@/views/${view}`], resolve);
  7. let rootRouter = [
  8. {
  9. path: "",
  10. redirect: { name: "/" },
  11. },
  12. {
  13. path: "/",
  14. name: "/",
  15. redirect: { name: "index" },
  16. children: [
  17. // 首页 必须 name:index
  18. // 刷新页面
  19. {
  20. path: "refresh",
  21. name: "refresh",
  22. hidden: true,
  23. component: {
  24. beforeRouteEnter(to, from, next) {
  25. next((vm) => vm.$router.replace(from.fullPath));
  26. },
  27. render: (h) => h(),
  28. },
  29. },
  30. // 页面重定向
  31. {
  32. path: "redirect/:route*",
  33. name: "redirect",
  34. hidden: true,
  35. component: {
  36. beforeRouteEnter(to, from, next) {
  37. next((vm) => vm.$router.replace(JSON.parse(from.params.route)));
  38. },
  39. render: (h) => h(),
  40. },
  41. },
  42. ],
  43. },
  44. ];
  45. let addRouters = {
  46. path: "/index",
  47. name: "index",
  48. redirect: { name: "home" },
  49. component: loadView("Index"),
  50. meta: {
  51. auth: true,
  52. },
  53. children: [],
  54. };
  55. let lastRouter = [
  56. {
  57. path: "/:pathMatch(.*)*",
  58. component: loadView("error/404"),
  59. meta: {
  60. icon: "",
  61. title: "404",
  62. auth: false,
  63. isDisable: true,
  64. isCache: false,
  65. },
  66. },
  67. {
  68. path: "/:pathMatch(.*)",
  69. component: loadView("error/404"),
  70. meta: {
  71. icon: "",
  72. title: "404",
  73. auth: false,
  74. isDisable: true,
  75. isCache: false,
  76. },
  77. }
  78. ];
  79. setItemRouter(addRouters.children, dataList, "");
  80. return [...rootRouter, addRouters, ...lastRouter];
  81. };
  82. const setItemRouter = (routerList, dataList, baseUrl) => {
  83. for (let data of dataList) {
  84. let path = baseUrl + "/" + data.path;
  85. let route = {
  86. path: path,
  87. name: data.path,
  88. redirect: "",
  89. component: loadView(data.component || "Index"),
  90. meta: {
  91. icon: "",
  92. title: data.title,
  93. auth: true,
  94. isSideMenu: !!data.isSideMenu,
  95. isCache: !data.isCache,
  96. },
  97. children: [],
  98. };
  99. if (data.children && data.children.length > 0) {
  100. route.redirect = { name: data.children[0].path };
  101. routerList.push(route);
  102. setItemRouter(routerList, data.children, path);
  103. } else {
  104. routerList.push(route);
  105. }
  106. }
  107. };
  108. export const loadView = (view) => {
  109. // 路由懒加载
  110. // return (resolve) => require([`@/views/${view}`], resolve);
  111. return () => Promise.resolve(require(`@/views/${view}`).default);
  112. };

4、*router:

routes.js:需要配置的白名单,也就是说包含登录、注册、404等页面

index.js:动态路由处理,由vue2中为addRoutes() 改为vue3中的addRoute(),核心代码:

  1. store
  2. .dispatch("store/user/getUserInfo")
  3. .then((resp) => {
  4. resp.forEach((route) => {
  5. router.addRoute(route);
  6. router.options.routes.push(route);
  7. });
  8. next({ ...to, replace: true });
  9. NProgress.done();
  10. })
  11. .catch((error) => {
  12. console.log(error);
  13. });

5、page-view:

剩下的就简单很多了,只需要处理菜单就ok了,项目已经写了一个菜单demo,无限级的。

vue3-pc:vue3+vue-router4+vuex+axios+elementplus

vue3-h5:vue3+vue-router4+vuex+axios+vant主要还是基于pc的架子做的移动适配,我不怎么做移动端,所以如果哪位有时间可以fork过去提交代码,目前这一版可以正常玩耍。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/379225
推荐阅读