TypeScript cannot handle type information for .vue imports by default, so we replace the tsc CLI with vue-tsc for type checking. In editors, we need TypeScript Vue Plugin (Volar) to make the TypeScript language service aware of .vue types.
If the standalone TypeScript plugin doesn’t feel fast enough to you, Volar has also implemented a Take Over Mode that is more performant. You can enable it by the following steps:
Disable the built-in TypeScript Extension
Run Extensions: Show Built-in Extensions from VSCode’s command palette
Find TypeScript and JavaScript Language Features, right click and select Disable (Workspace)
Reload the VSCode window by running Developer: Reload Window from the command palette.
vue3_ts_cms
This template should help get you started developing with Vue 3 in Vite.
Recommended IDE Setup
VSCode + Volar (and disable Vetur) + TypeScript Vue Plugin (Volar).
Type Support for
.vue
Imports in TSTypeScript cannot handle type information for
.vue
imports by default, so we replace thetsc
CLI withvue-tsc
for type checking. In editors, we need TypeScript Vue Plugin (Volar) to make the TypeScript language service aware of.vue
types.If the standalone TypeScript plugin doesn’t feel fast enough to you, Volar has also implemented a Take Over Mode that is more performant. You can enable it by the following steps:
Extensions: Show Built-in Extensions
from VSCode’s command paletteTypeScript and JavaScript Language Features
, right click and selectDisable (Workspace)
Developer: Reload Window
from the command palette.Customize configuration
See Vite Configuration Reference.
Project Setup
Compile and Hot-Reload for Development
Type-Check, Compile and Minify for Production
Lint with ESLint
配置vue文件类型声明
配置项目环境
配置项目代码规范 -见markdown
1.集成editorconfig配置 根目录创建.editorconfig文件 root = true [*] # 表示所有文件适用 charset = utf-8 # 设置文件字符集为 utf-8 indent_style = space # 缩进风格(tab | space) indent_size = 2 # 缩进大小 end_of_line = lf # 控制换行类型(lf | cr | crlf) trim_trailing_whitespace = true # 去除行尾的任意空白字符 insert_final_newline = true # 始终在文件末尾插入一个新行
2.使用prettier工具 Prettier 是一款强大的代码格式化工具,支持 JavaScript、TypeScript、CSS、SCSS、Less、JSX、Angular、Vue、GraphQL、JSON、Markdown 等语言,基本上前端能用到的文件格式它都可以搞定,是当下最流行的代码格式化工具。
1.安装prettier
2.配置.prettierrc文件:
none
,比如对象类型的最后一个属性后面是否加一个,;3.创建.prettierignore忽略文件
4.VSCode需要安装prettier的插件
5.VSCod中的配置
1.3. 使用ESLint检测
1.在前面创建项目的时候,我们就选择了ESLint,所以Vue会默认帮助我们配置需要的ESLint环境。
2.VSCode需要安装ESLint插件:
3.解决eslint和prettier冲突的问题:
安装插件:(vue在创建项目时,如果选择prettier,那么这两个插件会自动安装)
添加prettier插件:
4.VSCode中eslint的配置
项目目录结构划分
base-ui 其他项目公用组件 views 每个页面的视图 hooks 一些可复用的方法像钩子一样挂着,可以随时被引入和调用以实现高内聚低耦合的目标,应该都能算是hook; ……
CSS样式的重置
.对默认css样式进行重置: 在main.ts导入该样式 npm install normalize.css npm install reset.css .normalize.css
在assets/css中创建 .commen.less 复用文件 .index.less 入口文件 .reset.less 修改样式,在网上找一些就行
因为vite不能解析less文件,要安装依赖 npm install less -D
全家桶-路由配置
全家桶-状态管理
网络请求封装axios
1.安装axios: npm install axios 2.把之前封装好的库 复制过来
区分development (开发)和production (生成)环境
.对于有一些变量,它在开发环境下的值和生成环境下的值是不匹配的 例子: baseURL = “服务器地址” (开发环境服务器地址,和生成环境服务器地址不是同一个,是两台服务器甚至多台。) 有两台服务器对于前端来说 我们要保存的标识符在开发环境和生产环境用的不是同一个
1.Vite的环境变量 Vite在一个特殊的import.meta.env对象上暴露环境变量。这里有一些在所有情况下都可以使用的内建变量 import.meta.env.MODE:{string}应用运行的模式 import.meta.env.PROD:{boolean}应用是否运行在生产环境 import.meta.env.DEV:{boolean}应用是否运行在开发环境(永远于import.meta.env.PROD相反) import.meta.env.SSR:{boolean}应用是否运行在server上
2.vite使用dotenv从你的环境目录中的下列文件加载额外的环境变量 里面的变量名必须以VITE开头 .env文件 开发环境生产环境都会读取 .env.development 开发环境读取 .env.production 生产环境读取 .env.local 所有情况都会加载,但会被git忽略 .env.[mode] 只在指定模式下加载 .env.[mode].local 只在指定模式下加载 但会被git忽略
Element-Plus集成
1.安装element-plus: npm install element-plus 2.安装自动导入插件: npm install -D unplugin-vue-components unplugin-auto-import 3.官网有(然后把下列代码插入到你的 Vite 或 Webpack 的配置文件中)(配置时,指定文件生成位置:dts: ‘src/auto-imports.d.ts’ 另一个同上)
2.注意:2.当按需引入时会生成两个文件 auto-imports.d.ts components.d.ts 把这两个文件在tsconfig.app.json里配置 “include”: [“env.d.ts”, “src//*”, “src//*.vue”, “auto-imports.d.ts”, “components.d.ts”],
3.按需导入只会导入里面的组件,不会帮我们自动导入 ‘反馈组件’ 例如:Loading。因为这些组件是在