1.技术选型

wuhu项目基于Vue3.x和typeScript进行项目,使用Pinia、VueRouter4.x
资料:

1.1 构建工具

1.2 UI框架选择方案

1.3 CSS解决方案

2.环境搭建

(1).创建项目:

  1. yarn create vite wuhu-admin --template vue-ts
  2. cd wuhu-admin && yarn
  3. yarn add @types/node

在项目根目录下分别创建build和types文件夹,其中build用于存放Vite构建相关配置,types用于存放项目通用ts类型。创建完毕后目录如下:

  1. —— .vscode
  2. —— build
  3. —— node_modules
  4. —— public
  5. —— src
  6. —— build
  7. —— types
  8. —— .gitignore
  9. —— index.html
  10. —— package.json
  11. —— README.md
  12. —— tsconfig.json
  13. —— vite.config.ts
  14. —— yarn.lock

修改tsconfig.json,新增baseUrl、paths配置。

  1. {
  2. "compilerOptions": {
  3. "target": "esnext",
  4. "useDefineForClassFields": true,
  5. "module": "esnext",
  6. "moduleResolution": "node",
  7. "strict": true,
  8. "jsx": "preserve",
  9. "sourceMap": true,
  10. "resolveJsonModule": true,
  11. "esModuleInterop": true,
  12. "baseUrl": ".",
  13. "paths": {
  14. "/@/*": ["src/*"],
  15. "/#/*": ["types/*"]
  16. },
  17. "lib": ["esnext", "dom"]
  18. },
  19. "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
  20. }

2.1 安装VueRouter4.x

  1. yarn add vue-router@4

在项目src目录新建router文件夹,在router目录下新建types.ts、index.ts、routes/index.ts、guard/index.ts。
其中types.index是项目路由相关类型声明,index.ts提供了创建路由函数,routes/index.ts存放项目路由元信息,guard/index.ts存放路由守卫逻辑。

types.ts内容:

  1. import { RouteRecordRaw, RouteMeta } from "vue-router";
  2. import { defineComponent } from "vue";
  3. export type Component<T extends any = any> =
  4. | ReturnType<typeof defineComponent>
  5. | (() => Promise<typeof import("*.vue")>)
  6. | (() => Promise<T>);
  7. export interface AppRouteRecordRaw extends Omit<RouteRecordRaw, "meta"> {
  8. path: string;
  9. meta?: RouteMeta;
  10. component: Component | string;
  11. components?: Component;
  12. children?: AppRouteRecordRaw[];
  13. props?: object;
  14. fullPath?: string;
  15. }

routes/index.ts内容:

  1. import { AppRouteRecordRaw } from "../types";
  2. export const rootRoutes: AppRouteRecordRaw[] = [];

index.ts内容:

  1. import { createRouter, createWebHashHistory } from "vue-router";
  2. import { rootRoutes } from "./routes";
  3. export const router = createRouter({
  4. history: createWebHashHistory(),
  5. routes: rootRoutes,
  6. });

guard/index.ts内容:

  1. import { router } from "../index";
  2. // 页面守卫
  3. function createPageGuard() {
  4. router.beforeEach(() => {
  5. console.log("page 守卫");
  6. });
  7. router.afterEach(() => {});
  8. }
  9. export function createRouterGuard() {
  10. createPageGuard();
  11. }

2.2 安装Pinia

  1. yarn add pinia@next

在src目录创建下创建store文件夹,在store文件夹分别创建index.ts和modules,其中index.ts提供了store源,modules用于存放状态模块。
index.ts内容:

  1. import type { App } from "vue";
  2. import { createPinia } from "pinia";
  3. const store = createPinia();
  4. export function setupStore(app: App<Element>) {
  5. app.use(store);
  6. }
  7. export { store };

2.3 安装NaiveUI

  1. yarn add naive-ui vfonts

在src/components下新建registerGlobComp.ts,该文件中提供了全局安装Naive组件的函数。内容如下:

  1. /** 注册全局组件 */
  2. import { create } from "naive-ui";
  3. import * as naiveUI from "naive-ui";
  4. import { getKeyValue } from "../utils";
  5. const naiveComponents = Object.keys(naiveUI).map(
  6. (key) => /^N/.test(key) && getKeyValue(naiveUI, key as any)
  7. );
  8. /**
  9. * 全局注册naive ui 组件
  10. */
  11. export const registerNaiveComp = create({
  12. components: naiveComponents,
  13. });

2.4 安装TailwindCSS相关依赖

  1. yarn add tailwindcss@latest postcss@latest autoprefixer@latest

(1).创建tailwind.config.js

  1. const initSpacing = (
  2. num,
  3. unit = "px",
  4. isExcludeOdd = true
  5. ) => {
  6. const spacing = {};
  7. for (let i = 0; i <= num; i++) {
  8. const val = isExcludeOdd ? (i % 2 === 0 || i % 5 === 0) && i : i;
  9. if (val) {
  10. spacing[val] = val + unit;
  11. }
  12. }
  13. return spacing;
  14. };
  15. const initFontSize = (unit = "px") => {
  16. return {
  17. xs: `12${unit}`,
  18. sm: `14${unit}`,
  19. tiny: `15${unit}`,
  20. base: `16${unit}`,
  21. lg: `18${unit}`,
  22. xl: `20${unit}`,
  23. "2xl": `24${unit}`,
  24. "3xl": `30${unit}`,
  25. "4xl": `36${unit}`,
  26. "5xl": `48${unit}`,
  27. "6xl": `64${unit}`,
  28. "7xl": `80${unit}`,
  29. };
  30. };
  31. const initInset = (
  32. num,
  33. unit = "px",
  34. isExcludeOdd = true
  35. ) => {
  36. const inset = { "0": 0 };
  37. for (let i = -num; i <= num; i++) {
  38. const val = isExcludeOdd ? (i % 2 === 0 || i % 5 === 0) && i : i;
  39. if (val) {
  40. inset[val] = val + unit;
  41. }
  42. }
  43. return inset;
  44. };
  45. const initBorderRadius = (
  46. maxVal = 200,
  47. unit = "px",
  48. isExcludeOdd = true
  49. ) => {
  50. const radius = {
  51. none: "0",
  52. sm: `2${unit}`,
  53. DEFAULT: `4${unit}`,
  54. md: `6${unit}`,
  55. lg: `8${unit}`,
  56. xl: `12${unit}`,
  57. "2xl": `16${unit}`,
  58. "3xl": `24${unit}`,
  59. half: "50%",
  60. full: `9999${unit}`,
  61. };
  62. for (let i = 0; i <= maxVal; i++) {
  63. const val = isExcludeOdd ? (i % 2 === 0 || i % 5 === 0) && i : i;
  64. if (val) {
  65. radius[val] = val + unit;
  66. }
  67. }
  68. return radius;
  69. };
  70. const initColor = theme => {
  71. return {
  72. ...theme("colors"),
  73. primary: "#1E9FFF",
  74. error: "#FF5722",
  75. orange: "#FFB800",
  76. "light-blue": "#01AAED",
  77. green: "#009688",
  78. "light-green": "#5FB878",
  79. fa: "#FAFAFA",
  80. f6: "#F6F6F6",
  81. ee: "#eeeeee",
  82. e2: "#e2e2e2",
  83. dd: "#dddddd",
  84. d2: "#d2d2d2",
  85. cc: "#cccccc",
  86. c2: "#c2c2c2",
  87. };
  88. };
  89. /**
  90. * 生成width、height、line-height
  91. */
  92. const initSize = (
  93. maxValue = 300,
  94. unit = "px",
  95. isExcludeOdd = true
  96. ) => {
  97. const size = {};
  98. for (let i = 0; i <= maxValue; i++) {
  99. const val = isExcludeOdd ? (i % 2 === 0 || i % 5 === 0) && i : i;
  100. if (val) {
  101. size[val] = val + unit;
  102. }
  103. }
  104. return size;
  105. };
  106. module.exports = {
  107. purge: ["./src/**/*.html", "./src/**/*.{js,jsx,ts,tsx}"],
  108. darkMode: false, // or 'media' or 'class'
  109. theme: {
  110. theme: {
  111. borderRadius: initBorderRadius(),
  112. extend: {
  113. spacing: initSpacing(200),
  114. inset: initInset(200),
  115. lineHeight: initSize(300),
  116. },
  117. fontSize: initFontSize(),
  118. textColor: (theme) => initColor(theme),
  119. borderColor: (theme) => initColor(theme),
  120. },
  121. },
  122. variants: {
  123. extend: {},
  124. },
  125. plugins: [],
  126. };

(2).创建postcss.config.js。

  1. module.exports = {
  2. plugins: {
  3. tailwindcss: {},
  4. autoprefixer: {},
  5. }
  6. }

(3).在src目录下新建styles用于存放项目通用css文件,在styles目录下新建tailwind.css并引入tailwind.css。

  1. @tailwind base;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @layer base {
  5. /* 布局相关 */
  6. .flex-center {
  7. @apply flex justify-center items-center;
  8. }
  9. .inline-flex-center {
  10. @apply inline-flex justify-center items-center;
  11. }
  12. .flex-x-center {
  13. @apply flex justify-center;
  14. }
  15. .flex-y-center {
  16. @apply flex items-center;
  17. }
  18. .absolute-center {
  19. @apply absolute;
  20. top: 50%;
  21. left: 50%;
  22. transform: translate(-50%, -50%);
  23. }
  24. .absolute-x-center {
  25. @apply absolute;
  26. left: 50%;
  27. transform: translateX(-50%);
  28. }
  29. .absolute-y-center {
  30. @apply absolute;
  31. top: 50%;
  32. transform: translateY(-50%);
  33. }
  34. .overflow-hidden-line-1 {
  35. text-overflow: ellipsis;
  36. display: -webkit-box;
  37. overflow: hidden;
  38. -webkit-box-orient: vertical;
  39. -webkit-line-clamp: 1;
  40. }
  41. .overflow-hidden-line-2 {
  42. text-overflow: ellipsis;
  43. display: -webkit-box;
  44. -webkit-box-orient: vertical;
  45. overflow: hidden;
  46. -webkit-line-clamp: 2;
  47. }
  48. .overflow-hidden-line-3 {
  49. text-overflow: ellipsis;
  50. display: -webkit-box;
  51. -webkit-box-orient: vertical;
  52. overflow: hidden;
  53. -webkit-line-clamp: 3;
  54. }
  55. }

(4).在main.ts引入styles目录下的tailwind.css。

  1. import "./styles/tailwind.css";

2.5 在main.ts引入各种依赖

  1. import { createApp } from "vue";
  2. import App from "./App.vue";
  3. import { store } from "./store";
  4. import { router } from "./router";
  5. import { createRouterGuard } from "./router/guard";
  6. import { registerNaiveComp } from "./components/registerGlobComp";
  7. import "vfonts/Lato.css";
  8. import "vfonts/FiraCode.css";
  9. import "./styles/tailwind.css";
  10. const bootstrap = async () => {
  11. const app = createApp(App);
  12. app.use(store);
  13. app.use(router);
  14. app.use(registerNaiveComp);
  15. createRouterGuard();
  16. app.mount("#app");
  17. };
  18. await bootstrap();

2.6 安装prettier

# pretty-quick作用是在变化的文件上运行prettier进行格式化,后面会在package.json配置脚本
yarn add prettier pretty-quick --dev

(1).创建perttier配置文件。项目根目录下新建.prettierrc.js。

module.exports = {
  printWidth: 80, // 每行代码长度(默认80)
  tabWidth: 2, // 每个tab相当于多少个空格(默认2)
  useTabs: false, // 是否使用tab进行缩进(默认false)
  singleQuote: true, // 使用单引号(默认false)
  semi: true, // 声明结尾使用分号(默认true)
  trailingComma: 'all', // 多行使用拖尾逗号(默认none)
  bracketSpacing: true, // 对象字面量的大括号间使用空格(默认true)
  jsxBracketSameLine: false, // 多行JSX中的>放置在最后一行的结尾,而不是另起一行(默认false)
  arrowParens: 'avoid', // 只有一个参数的箭头函数的参数是否带圆括号(默认avoid)
};

(2).新建perttier忽略文件。项目根目录下新建.prettierignore。

/dist/*
.local
.output.js
/node_modules/**

**/*.svg
**/*.sh

/public/*

2.7 安装eslint

yarn add eslint eslint-define-config eslint-config-prettier eslint-plugin-prettier eslint-plugin-vue vue-eslint-parser @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-jest --dev

依赖说明如下:

  • eslint:eslint依赖。
  • eslint-define-config:为.eslintrc.js提供一个defineConfig函数,用于定义eslint配置文件。
  • eslint-config-prettier:关闭EsLint所有不必要的或可能与Prettier冲突的规则。
  • eslint-plugin-prettier:运行Prettier作为ESLint规则,并报告差异个别ESLint问题。
  • eslint-plugin-vue:Vue官方的ESLint插件,这个插件可以检查