1. 创建一个vite项目
pnpm create vite
√ Project name: ... ( 项目名)
√ Select a framework: » Vue
√ Select a variant: » TypeScript
Scaffolding project in D : \vite- project...
Done. Now run:
cd ( 项目名)
pnpm install
pnpm run dev
2. Eslint+Prettier安装及配置
2.1 Eslint安装及初始化
pnpm add - D eslint
npx eslint -- init
√ How would you like to use ESLint? · problems
√ What type of modules does your project use? · esm
√ Which framework does your project use? · vue
√ Does your project use TypeScript? · No / Yes
√ Where does your code run? · browser, node
√ What format do you want your config file to be in ? · JavaScript
The config that you've selected requires the following dependencies:
eslint- plugin- vue@latest @typescript- eslint/ eslint- plugin@latest @typescript- eslint/ parser@latest
√ Would you like to install them now? · No / Yes
√ Which package manager do you want to use? · pnpm
Installing eslint- plugin- vue@latest, @typescript- eslint/ eslint- plugin@latest, @typescript- eslint/ parser@latest
"scripts" : {
"lint" : "eslint . --ext .vue,.js,.ts,.jsx,.tsx --fix" ,
} ,
pnpm lint
> vite- project@0.0 .0 lint D : \vite- project
> eslint . -- ext . vue, . js, . ts, . jsx, . tsx -- fix
D : \vite- project\src\App. vue
1 : 8 error Parsing error: '>' expected
D : \vite- project\src\components\HelloWorld. vue
1 : 8 error Parsing error: '>' expected
✖ 2 problems ( 2 errors, 0 warnings)
ELIFECYCLE Command failed with exit code 1.
2.2 Prettier安装及配置
pnpm add - D prettier eslint- plugin- prettier eslint- config- prettier
# https: / / editorconfig. org
root = true
[ * ]
charset = utf- 8
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[ * . md]
indent_style = tab
insert_final_newline = false
trim_trailing_whitespace = false
module. exports = {
printWidth : 80 ,
tabWidth : 4 ,
useTabs : false ,
semi : false ,
singleQuote : true ,
}
module. exports = {
env : {
browser : true ,
es2021 : true ,
node : true ,
'vue/setup-compiler-macros' : true ,
} ,
extends : [
'eslint:recommended' ,
'plugin:vue/vue3-recommended' ,
'plugin:@typescript-eslint/recommended' ,
'plugin:prettier/recommended' ,
] ,
overrides : [ ] ,
parser : 'vue-eslint-parser' ,
parserOptions : {
ecmaVersion : 'latest' ,
parser : '@typescript-eslint/parser' ,
sourceType : 'module' ,
} ,
plugins : [ 'vue' , '@typescript-eslint' ] ,
rules : { } ,
}
"scripts" : {
"format" : "prettier --write ./**/*.{vue,ts,tsx,js,jsx,css,less,scss,json,md}"
} ,
pnpm format
> vite- project@0.0 .0 format D : \vite- project
> prettier -- write . * . { vue, ts, tsx, js, jsx, css, less, scss, json, md}
. vscode\extensions. json 47ms
package . json 6ms
README . md 53ms
src\App. vue 249ms
src\components\HelloWorld. vue 49ms
src\main. ts 7ms
src\style. css 19ms
src\vite- env. d. ts 9ms
tsconfig. json 6ms
tsconfig. node. json 3ms
vite. config. ts 6ms
3. 基础依赖(Pinia+VueRouter+Axios)
3.1 Pinia安装
pnpm add pinia pinia- plugin- persistedstate
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
createApp ( App) . use ( createPinia ( ) . use ( piniaPluginPersistedstate) ) . mount ( '#app' )
3.1 VueRouter安装及配置
pnpm add vue- router
< script setup lang= "ts" > < / script>
< template>
< router- view / >
< / template>
< style scoped lang= "scss" > < / style>
< script setup lang= "ts" >
import HelloWorld from '../components/HelloWorld.vue'
< / script>
< template>
< div>
< a href= "https://vitejs.dev" target= "_blank" >
< img src= "/vite.svg" class = "logo" alt= "Vite logo" / >
< / a>
< a href= "https://vuejs.org/" target= "_blank" >
< img src= "./assets/vue.svg" class = "logo vue" alt= "Vue logo" / >
< / a>
< / div>
< HelloWorld msg= "Vite + Vue" / >
< / template>
< style scoped>
. logo {
height : 6em;
padding : 1 . 5em;
will- change: filter;
transition : filter 300ms;
}
. logo: hover {
filter : drop- shadow ( 0 0 2em #646cffaa) ;
}
. logo. vue: hover {
filter : drop- shadow ( 0 0 2em #42b883aa) ;
}
< / style>
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
const routes : Array< RouteRecordRaw> = [
{
path : '/' ,
name : 'index' ,
component : ( ) => import ( './views/index.vue' ) ,
meta : {
title : '首页' ,
} ,
} ,
]
createApp ( App)
. use ( createPinia ( ) . use ( piniaPluginPersistedstate) )
. use (
createRouter ( {
history : createWebHashHistory ( ) ,
routes : routes,
} )
)
. mount ( '#app' )
3.2 Axios安装及配置
pnpm add axios
import axios, { AxiosRequestConfig } from 'axios'
export const createAxios = ( axiosConfig : AxiosRequestConfig) => {
const Axios = axios. create ( {
baseURL : '' ,
timeout : 1000 * 10 ,
headers : {
'Content-Type' : 'application/json' ,
} ,
responseType : 'json' ,
method : 'POST' ,
} )
Axios. interceptors. request. use (
( config ) => {
return config
} ,
( error ) => {
return Promise. reject ( error)
}
)
Axios. interceptors. response. use (
( response ) => {
return response. data
} ,
( error ) => {
return Promise. reject ( error)
}
)
return Axios ( axiosConfig)
}