深色模式
后台应用 实践笔记
说明
练习搭建一个简单的后台应用
技术选型
技术栈 | 官网/文档 | 笔记 | 项目案例/备注 |
---|---|---|---|
官网中文 | - | 前端工具链 | |
官网中文 | - | 前端 JavaScript 框架 | |
官网中文 | - | Vue.js 路由 | |
官网中文 | - | Vue.js 状态管理库 | |
官网中文 | - | 基于 promise 的网络请求库 | |
官网中文 | - | Vue.js 组件库 |
初期安装及配置
构建一个 Vite + Vue 项目
Bash
# npm 7+, extra double-dash is needed:
npm create vite@latest admin-vue-app -- --template vue-ts
1
2
2
Bash
cd admin-vue-app
npm install
npm run dev
1
2
3
2
3
安装其他技术栈
Bash
npm install vue-router@4 # 安装 Vue Router
1
Bash
npm install pinia # 安装 Pinia
1
Bash
npm install axios # 安装 Axios
1
Bash
npm install element-plus --save # 安装 Element Plus
1
在 Vite 中配置路径别名
修改 tsconfig.json
可让 VS Code 认识 @ 符号
json
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
}
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
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
安装 node 的类型声明文件
Bash
npm i @types/node -D
1
修改 vite.config.ts
ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// 这里使用 nodejs 的模块,可能会报错,需要安装 node 的类型声明文件
import path from 'path'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
//设置别名
alias: {
'@': path.resolve(__dirname, './src')
}
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
新建初始布局组件
新建 src/layout/index.vue
布局组件
vue
<template>
<h1>@/layout/index.vue</h1>
<router-view />
</template>
<script lang="ts" setup></script>
<style scoped></style>
1
2
3
4
5
6
2
3
4
5
6
新建 src/views/index.vue
首页组件
vue
<template>
<h1>@/views/index.vue</h1>
</template>
<script setup lang="ts"></script>
<style scoped></style>
1
2
3
4
5
2
3
4
5
新建 src/views/login.vue
登录组件
vue
<template>
<h1>@/views/login.vue</h1>
</template>
<script setup lang="ts"></script>
<style scoped></style>
1
2
3
4
5
2
3
4
5
Vue Router 加入项目
新建 src/router/routes.ts
路由映射到组件
ts
import Layout from '@/layout/index.vue'
const routes = [
{
path: '/',
redirect: '/index',
component: Layout,
children: [
{
path: '/index',
component: () => import('@/views/index.vue'),
}
]
},
{
path: '/login',
component: () => import('@/views/login.vue'),
}
]
export default routes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
新建 src/router/index.ts
创建路由实例
ts
import { createRouter, createWebHashHistory } from 'vue-router'
import routes from '@/router/routes'
// 创建路由实例并传递 `routes` 配置
export const router = createRouter({
// Hash 模式
history: createWebHashHistory(),
routes, // `routes: routes` 的缩写
})
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
修改 src/main.ts
使路由支持整个应用
ts
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import { router } from './router'
// createApp(App).mount('#app')
const app = createApp(App)
app.use(router)
app.mount('#app')
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
修改 src/App.vue
修改应用入口添加路由出口
vue
<template>
<router-view />
</template>
<script setup lang="ts"></script>
<style scoped></style>
1
2
3
4
5
2
3
4
5
Pinia 加入项目
修改 src/main.ts
创建一个 pinia 实例 (根 store) 并将其传递给应用
ts
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import './style.css'
import App from './App.vue'
import { router } from './router'
// createApp(App).mount('#app')
const app = createApp(App)
const pinia = createPinia()
app.use(pinia)
app.use(router)
app.mount('#app')
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
(Pinia 基础示例)新建 src/stores/counter.ts
先创建一个 Store
ts
import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('Counter', () => {
// sate
const count = ref(0)
// getters
const doubleCount = computed(() => count.value * 2)
// actions
function reset() {
count.value = 0
}
function randomizeCounter() {
count.value = Math.round(100 * Math.random())
}
const increment = () => {
count.value++
}
async function refresh() {
increment()
randomizeCounter()
}
return {
// sate
count,
// getters
doubleCount,
// actions
reset, randomizeCounter, increment, refresh,
}
})
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
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
(Pinia 基础示例)在 src/views/index.vue
组件中使用该 store
vue
<template>
<h1>@/views/index.vue</h1>
<div>counter.count={{ counter.count }}</div>
<div>counter.doubleCount = computed: {{ counter.doubleCount }}</div>
<button @click="counter.increment">counter.increment()</button>
<button @click="counter.randomizeCounter()">counter.randomizeCounter()</button>
<button @click="counter.reset()">counter.reset()</button>
</template>
<script setup lang="ts">
import { useCounterStore } from '@/stores/counter'
const counter = useCounterStore()
counter.count++
counter.increment()
</script>
<style scoped></style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Element Plus 加入项目
修改 src/main.ts
完整引入 Element Plus
ts
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus' //[!code ++]
import 'element-plus/dist/index.css' //[!code ++]
import App from './App.vue'
import { router } from './router'
const pinia = createPinia()
const app = createApp(App)
app.use(pinia)
// 在引入 ElementPlus 时,可以传入一个包含 size 和 zIndex 属性的全局配置对象。
// size 用于设置表单组件的默认尺寸,zIndex 用于设置弹出组件的层级,zIndex 的默认值为 2000。
app.use(ElementPlus, { size: 'small', zIndex: 3000 }) //[!code ++]
app.use(router)
app.mount('#app')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Volar 支持
如果您使用 Volar,请在 tsconfig.json
中通过 compilerOptions.type
指定全局组件类型。
json
// tsconfig.json
{
"compilerOptions": {
// ...
"types": ["element-plus/global"]
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
修改 src/layout/index.vue
使用 Container 布局容器
vue
<template>
<el-container>
<el-header><Header /></el-header>
<el-container>
<el-aside><Aside /></el-aside>
<el-main><router-view /></el-main>
</el-container>
</el-container>
</template>
<script lang="ts" setup>
import Header from '@/layout/Header.vue'
import Aside from '@/layout/Aside.vue'
</script>
<style scoped></style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
最后也可以直接使用 Element Plus 提供的 Vite 模板,快速搭建项目 https://github.com/element-plus/element-plus-vite-starter
预览:https://vite-starter.element-plus.org
Bash
#
git clone https://github.com/element-plus/element-plus-vite-starter
cd element-plus-vite-starter
npm i
npm run dev
1
2
3
4
5
2
3
4
5