1.技术选型
wuhu项目基于Vue3.x和typeScript进行项目,使用Pinia、VueRouter4.x
资料:
- Vue3风格指南:https://v3.cn.vuejs.org/style-guide/
- Vue代码提交规范:https://github.com/vuejs/vue/blob/dev/.github/COMMIT_CONVENTION.md
1.1 构建工具
1.2 UI框架选择方案
1.3 CSS解决方案
2.环境搭建
(1).创建项目:
yarn create vite wuhu-admin --template vue-tscd wuhu-admin && yarnyarn add @types/node
在项目根目录下分别创建build和types文件夹,其中build用于存放Vite构建相关配置,types用于存放项目通用ts类型。创建完毕后目录如下:
—— .vscode—— build—— node_modules—— public—— src—— build—— types—— .gitignore—— index.html—— package.json—— README.md—— tsconfig.json—— vite.config.ts—— yarn.lock
修改tsconfig.json,新增baseUrl、paths配置。
{"compilerOptions": {"target": "esnext","useDefineForClassFields": true,"module": "esnext","moduleResolution": "node","strict": true,"jsx": "preserve","sourceMap": true,"resolveJsonModule": true,"esModuleInterop": true,"baseUrl": ".","paths": {"/@/*": ["src/*"],"/#/*": ["types/*"]},"lib": ["esnext", "dom"]},"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]}
2.1 安装VueRouter4.x
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内容:
import { RouteRecordRaw, RouteMeta } from "vue-router";import { defineComponent } from "vue";export type Component<T extends any = any> =| ReturnType<typeof defineComponent>| (() => Promise<typeof import("*.vue")>)| (() => Promise<T>);export interface AppRouteRecordRaw extends Omit<RouteRecordRaw, "meta"> {path: string;meta?: RouteMeta;component: Component | string;components?: Component;children?: AppRouteRecordRaw[];props?: object;fullPath?: string;}
routes/index.ts内容:
import { AppRouteRecordRaw } from "../types";export const rootRoutes: AppRouteRecordRaw[] = [];
index.ts内容:
import { createRouter, createWebHashHistory } from "vue-router";import { rootRoutes } from "./routes";export const router = createRouter({history: createWebHashHistory(),routes: rootRoutes,});
guard/index.ts内容:
import { router } from "../index";// 页面守卫function createPageGuard() {router.beforeEach(() => {console.log("page 守卫");});router.afterEach(() => {});}export function createRouterGuard() {createPageGuard();}
2.2 安装Pinia
yarn add pinia@next
在src目录创建下创建store文件夹,在store文件夹分别创建index.ts和modules,其中index.ts提供了store源,modules用于存放状态模块。
index.ts内容:
import type { App } from "vue";import { createPinia } from "pinia";const store = createPinia();export function setupStore(app: App<Element>) {app.use(store);}export { store };
2.3 安装NaiveUI
yarn add naive-ui vfonts
在src/components下新建registerGlobComp.ts,该文件中提供了全局安装Naive组件的函数。内容如下:
/** 注册全局组件 */import { create } from "naive-ui";import * as naiveUI from "naive-ui";import { getKeyValue } from "../utils";const naiveComponents = Object.keys(naiveUI).map((key) => /^N/.test(key) && getKeyValue(naiveUI, key as any));/*** 全局注册naive ui 组件*/export const registerNaiveComp = create({components: naiveComponents,});
2.4 安装TailwindCSS相关依赖
yarn add tailwindcss@latest postcss@latest autoprefixer@latest
(1).创建tailwind.config.js
const initSpacing = (num,unit = "px",isExcludeOdd = true) => {const spacing = {};for (let i = 0; i <= num; i++) {const val = isExcludeOdd ? (i % 2 === 0 || i % 5 === 0) && i : i;if (val) {spacing[val] = val + unit;}}return spacing;};const initFontSize = (unit = "px") => {return {xs: `12${unit}`,sm: `14${unit}`,tiny: `15${unit}`,base: `16${unit}`,lg: `18${unit}`,xl: `20${unit}`,"2xl": `24${unit}`,"3xl": `30${unit}`,"4xl": `36${unit}`,"5xl": `48${unit}`,"6xl": `64${unit}`,"7xl": `80${unit}`,};};const initInset = (num,unit = "px",isExcludeOdd = true) => {const inset = { "0": 0 };for (let i = -num; i <= num; i++) {const val = isExcludeOdd ? (i % 2 === 0 || i % 5 === 0) && i : i;if (val) {inset[val] = val + unit;}}return inset;};const initBorderRadius = (maxVal = 200,unit = "px",isExcludeOdd = true) => {const radius = {none: "0",sm: `2${unit}`,DEFAULT: `4${unit}`,md: `6${unit}`,lg: `8${unit}`,xl: `12${unit}`,"2xl": `16${unit}`,"3xl": `24${unit}`,half: "50%",full: `9999${unit}`,};for (let i = 0; i <= maxVal; i++) {const val = isExcludeOdd ? (i % 2 === 0 || i % 5 === 0) && i : i;if (val) {radius[val] = val + unit;}}return radius;};const initColor = theme => {return {...theme("colors"),primary: "#1E9FFF",error: "#FF5722",orange: "#FFB800","light-blue": "#01AAED",green: "#009688","light-green": "#5FB878",fa: "#FAFAFA",f6: "#F6F6F6",ee: "#eeeeee",e2: "#e2e2e2",dd: "#dddddd",d2: "#d2d2d2",cc: "#cccccc",c2: "#c2c2c2",};};/*** 生成width、height、line-height*/const initSize = (maxValue = 300,unit = "px",isExcludeOdd = true) => {const size = {};for (let i = 0; i <= maxValue; i++) {const val = isExcludeOdd ? (i % 2 === 0 || i % 5 === 0) && i : i;if (val) {size[val] = val + unit;}}return size;};module.exports = {purge: ["./src/**/*.html", "./src/**/*.{js,jsx,ts,tsx}"],darkMode: false, // or 'media' or 'class'theme: {theme: {borderRadius: initBorderRadius(),extend: {spacing: initSpacing(200),inset: initInset(200),lineHeight: initSize(300),},fontSize: initFontSize(),textColor: (theme) => initColor(theme),borderColor: (theme) => initColor(theme),},},variants: {extend: {},},plugins: [],};
(2).创建postcss.config.js。
module.exports = {plugins: {tailwindcss: {},autoprefixer: {},}}
(3).在src目录下新建styles用于存放项目通用css文件,在styles目录下新建tailwind.css并引入tailwind.css。
@tailwind base;@tailwind components;@tailwind utilities;@layer base {/* 布局相关 */.flex-center {@apply flex justify-center items-center;}.inline-flex-center {@apply inline-flex justify-center items-center;}.flex-x-center {@apply flex justify-center;}.flex-y-center {@apply flex items-center;}.absolute-center {@apply absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);}.absolute-x-center {@apply absolute;left: 50%;transform: translateX(-50%);}.absolute-y-center {@apply absolute;top: 50%;transform: translateY(-50%);}.overflow-hidden-line-1 {text-overflow: ellipsis;display: -webkit-box;overflow: hidden;-webkit-box-orient: vertical;-webkit-line-clamp: 1;}.overflow-hidden-line-2 {text-overflow: ellipsis;display: -webkit-box;-webkit-box-orient: vertical;overflow: hidden;-webkit-line-clamp: 2;}.overflow-hidden-line-3 {text-overflow: ellipsis;display: -webkit-box;-webkit-box-orient: vertical;overflow: hidden;-webkit-line-clamp: 3;}}
(4).在main.ts引入styles目录下的tailwind.css。
import "./styles/tailwind.css";
2.5 在main.ts引入各种依赖
import { createApp } from "vue";import App from "./App.vue";import { store } from "./store";import { router } from "./router";import { createRouterGuard } from "./router/guard";import { registerNaiveComp } from "./components/registerGlobComp";import "vfonts/Lato.css";import "vfonts/FiraCode.css";import "./styles/tailwind.css";const bootstrap = async () => {const app = createApp(App);app.use(store);app.use(router);app.use(registerNaiveComp);createRouterGuard();app.mount("#app");};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插件,这个插件可以检查和
