Vue 3 + TypeScript + Vite+ VueRouter + Axios + ElementPlus + Sass 创建 Vite 项目 普通创建 npm create 是 npm init
的别名
1 2 3 4 5 6 7 8 npm create vite@latest yarn create vite pnpm create vite
模板创建 1 2 3 4 5 6 7 8 9 10 11 npm create vite@latest my-vue-app --template vue npm create vite@latest my-vue-app -- --template vue yarn create vite my-vue-app --template vue pnpm create vite my-vue-app --template vue
查看 create-vite 以获取每个模板的更多细节:vanilla
,vanilla-ts
,vue
,vue-ts
,react
,react-ts
,preact
,preact-ts
,lit
,lit-ts
,svelte
,svelte-ts
。
1 2 npm create vite@latest my-vue-app -- --template vue-ts
安装依赖
运行
使用@路径的别名配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import { resolve } from 'path' export default defineConfig ({ resolve : { alias : [ { find : '@' , replacement : resolve (__dirname, './src' ) } ], }, });
1 2 3 4 5 6 7 8 9 10 11 { "compilerOptions" : { "baseUrl" : "." , "paths" : { "@/*" : ["src/*" ] } }, }
配置本地服务器 1 2 3 4 5 6 7 8 9 10 export default defineConfig ({ server : { host : 'localhost' , port : 8080 , strictPort : false , } });
使用 ElementPlus 安装 1 2 3 4 5 6 7 8 npm install element-plus --save yarn add element-plus pnpm install element-plus
完整引入 1 2 3 4 5 6 7 8 9 10 import { createApp } from 'vue' import ElementPlus from 'element-plus' import 'element-plus/dist/index.css' import App from './App.vue' const app = createApp (App )app.use (ElementPlus ) app.mount ('#app' )
按需引入 您需要使用额外的插件来导入要使用的组件
自动导入(推荐) 首先你需要安装unplugin-vue-components
和 unplugin-auto-import
这两款插件
1 npm install -D unplugin-vue-components unplugin-auto-import
然后把下列代码插入到你的 Vite
的配置文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import { defineConfig } from 'vite' 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 ()], }), ], })
手动导入 需要安装 unplugin-element-plus 来导入样式。 请参考 文档 了解如何配置它
1 2 3 4 5 6 7 8 9 <template> <el-button>I am ElButton</el-button> </template> <script> import { ElButton } from 'element-plus' export default { components: { ElButton }, } </script>
1 2 3 4 5 6 7 8 import { defineConfig } from 'vite' import ElementPlus from 'unplugin-element-plus/vite' export default defineConfig ({ plugins : [ElementPlus ()], })
使用 SASS 安装 1 npm install --save-dev sass
编写全局css变量/全局mixin 1 2 3 4 $font-size-normal : 32px; $bg-color : #1989fa;
1 2 3 4 5 6 @mixin box-shadow ($bulr: 20px, $color: #1989fa7a ) { -webkit-box-shadow : 0px 0px $bulr $color; -moz-box-shadow : 0px 0px $bulr $color; box-shadow : 0px 0px $bulr $color; }
vite引入并使用 1 2 3 4 5 6 7 8 9 10 11 12 13 css : { preprocessorOptions : { scss : { additionalData : '@import "@/styles/globalVar.scss";' , } } },
1 2 3 4 5 6 7 8 9 10 11 <!-- 按需引入并使用 --> <style scoped lang="scss"> @import "@/styles/globalMixin.scss"; .test{ width: 650px; height: 60px; font-size: $font-size-normal; background-color: $bg-color; @include box-shadow; } </style>
使用 VueRouter 安装 1 npm install vue-router@4
创建路由 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 import { createRouter, RouteRecordRaw , createWebHashHistory } from 'vue-router' ;import HomeView from '../views/HomeView.vue' ;const routes : Array <RouteRecordRaw > = [ { path : '/' , redirect : '/home' , }, { path : '/home' , name : 'home' , component : HomeView , }, ]; const router = createRouter ({ history : createWebHashHistory (), routes, }); export default router;
引入使用 1 2 3 4 5 6 7 8 9 import { createApp } from 'vue' import App from './App.vue' import router from './router' const app = createApp (App )app.use (router) app.mount ('#app' )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <script setup lang="ts"> import { useRouter } from 'vue-router'; const router = useRouter(); const fn = function () { router.push('/abc') } </script> <template> <button @click="fn">编程式导航</button> <router-link to="/home">路由组件导航</router-link> <!-- 路由出口 --> <!-- 路由匹配到的组件将渲染在这里 --> <router-view></router-view> </template>
使用 Vuex 安装 1 2 3 4 5 npm install vuex@next --save yarn add vuex@next --save
创建 Store (状态管理库) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import { createStore } from 'vuex' export default createStore ({ state () { return { count : 0 } }, mutations : { increment (state) { state.count ++ } } })
引入使用 1 2 3 4 5 6 7 8 9 import { createApp } from 'vue' import App from './App.vue' import store from './store' const app = createApp (App )app.use (store) app.mount ('#app' )
1 2 3 4 5 6 7 8 9 10 11 12 <script setup lang="ts"> import { useStore } from 'vuex'; const store = useStore(); const fn = function () { store.commit('increment'); }; </script> <template> <button @click="fn">按钮</button> <p>{{ store.state.count }}</p> </template>
使用 Axios 安装 1 2 3 4 5 6 7 8 npm install axios bower install axios yarn add axios
封装axios请求 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 import axios from 'axios' ;const request = axios.create ({ }); axios.interceptors .request .use ( function (config ) { return config; }, function (error ) { return Promise .reject (error); } ); axios.interceptors .response .use ( function (response ) { return response; }, function (error ) { return Promise .reject (error); } ); export default request;
1 2 3 4 5 6 7 8 9 import request from '@/utils/request' ;export function getCS ( ) { return request ({ method : 'get' , url : '/cs' , }); }
使用 1 2 3 4 5 6 7 8 9 10 11 <script setup lang="ts"> import { getCS } from '@/api/cs'; const getData = function () { getCS() }; </script> <template> <button @click="getData">发送请求</button> </template>
使用 eslint 安装 1 2 npm install eslint --save-dev
初始化 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 npx eslint --init You can also run this command directly using 'npm init @eslint/config' .? How would you 1ike to use ESLint ? ..- To check syntax only To check syntax and find problems > To check syntax,find problems,and enforce code style ? what type of modules does your project use? . > JavaScript modules (import /export ) CommonJS (require /exports ) None of these ? Which framework does your project use? React > Vue .js None of these ? Does your project use TypeScript ? No / Yes ? Mhere does your code run? ... (Press 〈space> to select,<a> to toggle all, is to invert selection) √ Browser √ Node ? How would you like to define a style for your project? > Use a popular style guide Answer questions about your style ? Which style guide do you want to fo11ow?. > Standard : https : XO : https : ? What format do you want your config file to be in ? > JavaScript YAML JSON "Checking peerDependencies of eslint-config-standard-with-typescript@latestThe config that you've selected requires the following dependencies:" eslint-plugin-vue@latest eslint-config-standard-with -typescript@latest @typescript-eslint/eslint-plugin@^5.0 .0 eslint@^8.0 .1 eslint-plugin-import @^2.25 .2 eslint-plugin-n@15.0 .0 eslint-plugin-promise6.0 .0 typescript@* ? would you like to install them now? No / Yes ? which package manager do you want to use? > npm yarn pnpm "Installing eslint-plugin-vue@latest,eslint-config-standard-with-typescript@latest,@typescript-eslint/eslint-plugin@^5.0.0,eslint@^8.0.1,eslint-plugin-import@'2.25.2, eslint-plugin-n@^15.0.0, eslint-plugin-promise@~6.0.0,typescript@*" added 97 packages,and changed 1 package in 19s Successfully created .eslintrc .cjs file in C :\Users \admin\Desktop \vite-vue-ts
配置默认格式化程序 1 搜索设置框贴入editor.defaultFormatter
目录结构 使用一个Node模块来自动完成这个任务:mddir
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 npm install mddir -D cd node_modules/mddir/src node mddir "../../../" |-- undefined |-- .gitignore |-- index.html |-- package-lock.json |-- package.json |-- README .md |-- tsconfig.json |-- tsconfig.node .json |-- vite.config .ts |-- .vscode | |-- extensions.json |-- public | |-- vite.svg |-- src |-- App .vue |-- main.ts |-- style.css |-- vite-env.d .ts |-- assets | |-- vue.svg |-- components | |-- HelloWorld .vue |-- router | |-- index.ts |-- styles | |-- globalMixin.scss | |-- globalVar.scss | |-- theme |-- views |-- HomeView .vue