一、Node版本管理 1.1 搭建node 1.1.1 Windows 准备nvm-windows ,是windows系统用来管理node版本的工具。使用过node的应该都知道,node新版本是完全不兼容旧版本的,这就是这个工具存在的意义
1 2 3 4 5 6 7 8 9 nvm list nvm install 16.16.0 nvm use 16.16.0 node -v npm -v
操作如图
1.1.2 Linux 保证配置代理的基础上,执行如下命令
1 2 3 4 5 6 7 curl -L -o a.tar.gz https://github.com/nvm-sh/nvm/archive/refs/tags/v0.39.2.tar.gz && tar -zxvf a.tar.gz && mv nvm-0.39.2/ /usr/local /nvm cat > /etc/profile.d/nvm.sh <<EOF #!/usr/bin/env bash export NVM_DIR="/usr/local/nvm" [ -s "\$NVM_DIR/nvm.sh" ] && \. "\$NVM_DIR/nvm.sh" EOF source /etc/profile && nvm --version
操作如图
参考CentOS7下安装NVM_centos 安装nvm_veejaLiu的博客-CSDN博客
1.2 创建vue项目 参考文档简介 | Vue.js ,执行命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 C:\Users\meethigher\Desktop>npm init vue@latest npm WARN config global `--global`, `--local ` are deprecated. Use `--location=global` instead. Vue.js - The Progressive JavaScript Framework √ Project name: ... vue-admin √ Add TypeScript? ... No √ Add JSX Support? ... No √ Add Vue Router for Single Page Application development? ... Yes √ Add Pinia for state management? ... No √ Add Vitest for Unit Testing? ... No √ Add an End-to-End Testing Solution? » No √ Add ESLint for code quality? ... No Scaffolding project in C:\Users\meethigher\Desktop\vue-admin... Done. Now run: cd vue-admin npm install npm run dev
按提示命令,启动项目
二、项目理解 2.1 配置IDE 作为一个后端人员来说,还是更喜欢jetbrains家的产品,此处使用的webstorm。由于写js习惯性带;
,习惯用""
,所以需要配置自动添加分号。
进行ctrl+alt+L
格式化代码时,即可自动添加。
2.2 理解代码 1.) 理解main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import {createApp} from "vue" ;import App from "./App.vue" ;import router from "./router" ;import "./assets/main.css" ;const app = createApp(App);app.use(router); app.mount("#app" );
2.) 理解router.js,参考文档介绍 | Vue Router
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import {createRouter, createWebHistory, createWebHashHistory} from "vue-router" ;import HomeView from "../views/HomeView.vue" ;const routers = [ { path: "/" , name: "home" , component: HomeView }, { path: "/about" , name: "about" , component: () => import ("../views/AboutView.vue" ) } ]; const router = createRouter({ history: createWebHistory(import .meta.env.BASE_URL), routes: routers }); export default router;
3.) 理解app.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 <script setup > import {RouterLink, RouterView} from "vue-router" ; import HelloWorld from "./components/HelloWorld.vue" ; </script > <template > <header > <div class ="wrapper" > <HelloWorld msg ="You did it!" /> <nav > <RouterLink to ="/" > Home</RouterLink > <RouterLink to ="/about" > About</RouterLink > </nav > </div > </header > <RouterView /> </template > <style scoped > header { line-height : 1.5 ; max-height: 100vh; } .logo { display: block; margin: 0 auto 2rem; } nav { width: 100%; font-size: 12px; text-align: center; margin-top: 2rem; } nav a .router-link-exact-active { color: var(--color-text); } nav a .router-link-exact-active :hover { background-color: transparent; } nav a { display: inline-block; padding: 0 1rem; border-left: 1px solid var(--color-border); } nav a :first-of-type { border: 0; } @media (min-width: 1024px ) { header { display: flex; place-items: center; padding-right: calc(var(--section-gap) / 2); } .logo { margin: 0 2rem 0 0; } header .wrapper { display: flex; place-items: flex-start; flex-wrap: wrap; } nav { text-align: left; margin-left: -1rem; font-size: 1rem; padding: 1rem 0; margin-top: 1rem; } } </style >
三、搭建项目 3.1 置空项目 将不必要的文件都删除,最后的结构如图
启动项目后是个空的
3.2 安装css预编译less 安装
使用
1 2 3 4 <style scoped lang ="less" > </style >
3.3 安装全局初始化样式 normalize - npm
安装
在main.js中全局引用
1 2 import "normalize.css/normalize.css" ;
3.4 安装element-plus 在element-plus中,元素名恰好是对应的class名称,可以直接配样式
快速开始 | Element Plus
安装
1 npm install element-plus
配置自动按需导入
1 npm install -D unplugin-vue-components unplugin-auto-import
按照官网要求修改vite.config.js
3.5 安装element-icon Icon 图标 | Element Plus 安装
1 npm install @element-plus/icons-vue
实际使用时,手动按需引入即可,如下
1 import {Fold, Expand} from "@element-plus/icons-vue" ;
3.6 安装axios 配置axios Axios 安装
封装通用request.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 import axios from "axios" ;const isDev = process.env.NODE_ENV == "development" ;const dev = "http://127.0.0.1/api/" ;const pro = "http://121.89.205.189:3000/" ;const request = axios.create({ baseURL: isDev ? dev : pro, timeout: 60000 , headers: {"X-Custom-Header" : "foobar" } }); request.interceptors.request.use(function (config ) { return config; }, function (error ) { return Promise .reject(error); }); request.interceptors.response.use(function (response ) { return response; }, function (error ) { return Promise .reject(error); }); export default function ajax (config ) { const {url = "" , method = "GET" , data = {}, headers = {}} = config; switch (method.toUpperCase()) { case "GET" : return request.get(url, { params: data }); case "POST" : if (headers["content-type" ] == "application/x-www-form-url-encoded" ) { const obj = new URLSearchParams(); for (let key in data) { obj.append(key, data[key]); } return request.post(url, data, {headers}); } if (headers["content-type" ] == "multipart/form-data" ) { let obj = new FormData(); for (let key in data) { obj.append(key, data[key]); } return request.post(url, data, {headers}); } return request.post(url, data); } }
配置vite跨域 开发服务器选项 | Vite 官方中文文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 server: { port: 80 , open: false , base: "./ " , proxy: { '^/api' : { target: 'http://121.89.205.189:3000/' , changeOrigin: true , rewrite: (path ) => path.replace(/^\/api/ , '' ) } } }
3.7 安装vuex 配置vuex Vuex 是什么? | Vuex 安装
封装通用vuex.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 import {createStore} from "vuex" ;const store = createStore({ strict: process.env.NODE_ENV !== "production" , state() { return { count: 100 , userInfo: {} }; }, mutations: { add(state) { state.count++; }, addNum(state, amount) { state.count += amount; }, updateUserInfo(state, value) { state.userInfo = value; } }, actions: { addAction(context) { setTimeout(function ( ) { context.commit("add" ); }, 5000 ); }, addActionNum(context, amount) { setTimeout(function ( ) { context.commit("addNum" , amount); }, 5000 ); } }, getters: {}, modules: {} }); export default store;
vuex数据持久化 持久化vuex数据,方便在页面重新加载时,恢复数据。
通过自己手动存储需要持久化的数据 通过插件进行自动持久化 自动持久化,需要安装vuex插件
1 npm install vuex-persistedstate
添加plugins,如下图
使用生命周期函数验证登录 1 2 3 4 5 6 7 8 9 10 export default { mounted() { if (!this .$store.state.userInfo.adminname) { this .$router.push("/login" ); return ; } } }