创建项目并整理目录

1
npm init vue@latest

image.png

image-20250327193359034.png

git 管理项目 我放到gitee进行管理

什么是别名路径联想提示

在编写代码的过程中,一旦输入 @/,VSCode会立刻联想出src下所有子目录和文件,统一文件路径访问不容易出错

配置如下:

jsconfig.json配置别名路径

配置别名路径可以在写代码时联想提示路径

1
2
3
4
5
6
7
8
{
"compilerOptions" : {
"baseUrl" : "./",
"paths" : {
"@/*":["src/*"]
}
}
}

image-20250327194159871.png

elementPlus引入

image-20250327194257492.png

看文档如何 按需导入

1. 安装elementPlus和自动导入插件

1
2
npm install element-plus --save
npm install -D unplugin-vue-components unplugin-auto-import //D : 在开发环境中引入

2. 配置自动按需导入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 引入插件
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'


export default defineConfig({
plugins: [
// 配置插件
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
]
})

3. 测试组件

1
2
3
<template>
<el-button type="primary">i am button</el-button>
</template>

定制elementPlus主题

image-20250327195355614.png

1. 安装sass

基于vite的项目默认不支持css预处理器,需要开发者单独安装

1
npm i sass -D

2. 准备定制化的样式文件

从elementplus 下载

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
/* 只需要重写你需要的即可 */
@forward 'element-plus/theme-chalk/src/common/var.scss' with (
$colors: (
'primary': (
// 主色
'base': #27ba9b,
),
'success': (
// 成功色
'base': #1dc779,
),
'warning': (
// 警告色
'base': #ffb302,
),
'danger': (
// 危险色
'base': #e26237,
),
'error': (
// 错误色
'base': #cf4444,
),
)
)

3. 自动导入配置

这里自动导入需要深入到elementPlus的组件中,按照官方的配置文档来

  1. 自动导入定制化样式文件进行样式覆盖
  2. 按需定制主题配置 (需要安装 unplugin-element-plus)
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
import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
// 用于 增强 Vue.js 应用开发体验 的 Vite 插件

// elementPlus按需导入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig({
plugins: [
vue(),
vueDevTools(),
// 配置插件
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({

resolvers: [
// 1. 配置elementPlus采用sass样式配色系统
ElementPlusResolver({ importStyle: "sass" })
],
}),
],
resolve: {
// 实际的路径转换 @ -> src
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
},
},
css: {
preprocessorOptions: {
scss: {
// 自动导入定制化样式文件进行样式覆盖
additionalData: `
@use "@/styles/element/index.scss" as *;
`,
}
}
}
})

axios安装并简单封装

1. 安装axios

1
npm i axios

2. 基础配置

image-20250327200616856.png

官方文档地址:https://axios-http.com/zh/docs/intro
基础配置通常包括:

  1. 实例化 - baseURL + timeout
  2. 拦截器 - 携带token 401拦截等
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import axios from 'axios'

// 创建axios实例
const http = axios.create({
baseURL: 'https://pcapi-xiaotuxian-front-devtest.itheima.net/home/category/head',
timeout: 5000
})

// axios请求拦截器
instance.interceptors.request.use(config => {
return config
}, e => Promise.reject(e))

// axios响应式拦截器
instance.interceptors.response.use(res => res.data, e => {
return Promise.reject(e)
})


export default http

3. 封装请求函数并测试

1
2
3
4
5
6
7
import http from '@/utils/http'

export function getCategoryAPI () {
return http({
url: 'home/category/head'
})
}

如果不同的业务模块需要的接口基地址不同,该怎么做?

1
2
3
4
5
6
7
8
const http1 = axios.create({
baseURL: 'https://pcap2i-xiaotuxian-front-devtest.itheima.net/home/category/head',
timeout: 5000
})
const http2 = axios.create({
baseURL: 'https://pcapi-xiaotuxian-front-devtest.itheima.net/home/category/head',
timeout: 5000
})

路由整体设计

image-20250327202153109.png

路由设计原则:找页面的切换方式,如果是整体切换,则为一级路由,如果是在一级路由的内部进行的内容切换,则为二级路由

1
2
3
<template>
我是登录页
</template>
1
2
3
<template>
我是首页
</template>
1
2
3
<template>
我是home
</template>
1
2
3
<template>
我是分类
</template>
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
// createRouter:创建router实例对象
// createWebHistory:创建history模式的路由

import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login/index.vue'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'

const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
// path和component对应关系的位置
routes: [
{
path: '/',
component: Layout,
children: [
{
path: '',
component: Home
},
{
path: 'category',
component: Category
}
]
},
{
path: '/login',
component: Login
}
]
})

export default router

静态资源引入和Error Lens安装

image-20250327203806167.png

1. 静态资源引入

  1. 图片资源 - 把 images 文件夹放到 assets 目录下
  2. 样式资源 - 把 common.scss 文件放到 styles 目录下

2. Error Lens插件安装

image.png

scss变量自动导入

image-20250327204244747.png

1.新增一个var.scss 文件 存入色值变量

2.通过vite.config.js 配置自动导入文件

1
2
3
4
5
$xtxColor: #27ba9b;
$helpColor: #e26237;
$sucColor: #1dc779;
$warnColor: #ffb302;
$priceColor: #cf4444;
1
2
3
4
5
6
7
8
9
10
11
css: {
preprocessorOptions: {
scss: {
// 自动导入scss文件
additionalData: `
@use "@/styles/element/index.scss" as *;
@use "@/styles/var.scss" as *;
`,
}
}
}

测试

1
2
3
4
5
6
<style scoped lang="scss">
.test {
color: $priceColor;
}
</style>