文章所用技术栈:vite + vue3 + typescript + sass + vue-router + pinia + axios + ant Design Vue
创建vue3项目
输入项目名称

选择框架,按上下键选择vue

选择ts

执行提示的三条命令

ok 框架已经搭建好了
安装sass
使用的vite,它对sass、less、scss等文件都内置支持,所以我们只需要安装预处理器依赖就行
安装
安装vue-router
创建/src/router/index.ts并编写如下内容
不要忘记创建路由对应文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import { createRouter,createWebHashHistory } from "vue-router"; let routes= [ { path: '/', name: 'home', component: () => import('../views/Home/index.vue') }, { path: '/:pathMatch(.*)', name: '404', component: () => import('../views/404/index.vue'), } ] const router = createRouter({ history: createWebHashHistory(), routes }) export default router
|
修改main.ts文件引入router并使用
1 2 3 4 5 6 7 8
| import { createApp } from 'vue' import './style.css' import App from './App.vue'; import router from './router';
const app = createApp(App); app.use(router) app.mount('#app')
|
修改App.vue文件使用router-view组件
1 2 3 4 5 6 7 8 9 10
| <script setup lang="ts">
</script>
<template> <RouterView></RouterView> </template>
<style scoped> </style>
|
安装pinia
创建/src/store/counter.ts并编写如下内容.
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
| import { defineStore } from "pinia";; export const useCounter = defineStore('counter', { state: () => { return { counter: 0, } }, getters: { doubleCount: (state) => { return state.counter * 2 }, }, actions: { add(num:number) { this.counter+=num; }, asyncAdd(num:number) { return new Promise((resolve, reject) => { setTimeout(() => { try { this.counter += num; resolve('成功') } catch (e) { reject(e); } }, 1000) }) } } })
|
修改main.ts文件引入
1 2 3 4 5 6 7 8 9 10 11
| import { createApp } from 'vue' import './style.css' import App from './App.vue'; import router from './router'; import { createPinia } from 'pinia'
const pinia = createPinia()
const app = createApp(App); app.use(router).use(pinia); app.mount('#app')
|
**修改创建的/src/views/Home/index.vue **pinia使用
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
| <template> <div class=""> Home<br /> <div> count:{{ counter }} </div> <div> <input @click="count.add(1)" type="button" value="增加"> <input @click="count.asyncAdd(2)" type="button" value="异步增加"> </div> </div> </template>
<script setup lang="ts"> import { storeToRefs } from 'pinia';
import { useCounter } from '../../store/counter'; import { watch } from 'vue'; const count = useCounter();
let { counter, doubleCount } = storeToRefs(count); watch(counter, () => { console.log(doubleCount.value); })
setTimeout(() => { counter.value++; }, 2000)
count.add(1)
console.log(counter.value);
count.$patch((state) => { state.counter++; })
count.$onAction(({ name, store, args, after, onError }) => { console.log({ name, store, args, after, onError }) }) </script>
<style scoped></style>
|
安装pinia-plugin-persistedstate 实现数据持久化
1
| npm i pinia-plugin-persistedstate
|
修改main.ts文件
1 2 3 4 5 6 7 8 9 10 11 12
| import { createApp } from 'vue' import './style.css' import App from './App.vue'; import router from './router'; import { createPinia } from 'pinia' import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
const pinia = createPinia() pinia.use(piniaPluginPersistedstate) const app = createApp(App); app.use(router).use(pinia); app.mount('#app')
|
修改/src/store/counter.ts 添加参数
pinia-plugin-persistedstate
详细使用->pinia-plugin-persistedstate
安装axios
ts+vue封装axios
配置路径别名
修改vite.config.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import { resolve } from "path";
export default defineConfig({ plugins: [vue()], resolve: { alias: { "@": resolve(__dirname, "./src") } } })
|
修改tsconfig.json
1 2 3 4 5 6 7 8 9
| { "compilerOptions": { ... "baseUrl": ".", "paths": { "@/*": ["src/*"] } } }
|
安装 Ant Design Vue
1
| npm i ant-design-vue @ant-design/icons-vue -S
|
安装就好 引入的事情交给自动引入吧
自动导入
受够每次都要引入 ref、watch….等等了吧 哈哈哈哈 看看尤大推荐的插件吧,摆脱手动引入
vue3自动导入Api、组件