Vue 3 + TypeScript + Vite 2024年4月最新管理系统基建

Vue 3 + TypeScript + Vite 2024年4月最新管理系统基建

相关依赖

  • vue: ^3.4.21
  • vite: ^5.2.0
  • typescript: ^5.2.2
  • eslint: ^9.0.0

1. 初始化项目

1.1 node版本要求

node: v18.17.1

1.2. 创建项目

使用 PNPM:

# 创建项目
pnpm create vite vue3-element-template --template vue-ts
# 安装依赖
pnpm install
# 启动项目
cd my-vue-app
pnpm run dev
# 构建项目
pnpm run build

2. 配置 tsconfig.json

修改 tsconfig.json, 删除tsconfig.node.json

{"compilerOptions": {"target": "ES2020", // 将代码编译为ES2020版本的 JS"useDefineForClassFields": true, // 将 class 声明中的字段语义从 [[Set]] 变更到 [[Define]]"module": "ES2020", // 使用 ES Module 格式打包编译后的文件"lib": ["ES2020", "DOM", "DOM.Iterable"], // 使用 Node 的模块解析策略"skipLibCheck": true, // 跳过对 .d.ts 文件的类型检查/* Bundler mode */"moduleResolution": "bundler", // 使用 Node 的模块解析策略 , 一般配合上面的module"allowImportingTsExtensions": true, // 允许 TypeScript 文件使用特定于 TypeScript 的扩展名(如 .ts、.mts 或 .tsx)相互导入。"resolveJsonModule": true, // 允许引入 JSON 文件"isolatedModules": true, // 要求所有文件都是 ES Module 模块。"noEmit": true, // 不输出文件,即编译后不会生成任何js文件"jsx": "preserve", // 保留原始的 JSX 代码,不进行编译/* Linting */"strict": true, // 开启所有严格的类型检查"noUnusedLocals": true, //报告未使用的局部变量的错误"noUnusedParameters": true, //报告函数中未使用参数的错误"noFallthroughCasesInSwitch": true, //确保switch语句中的任何非空情况都包含"allowJs": true, //允许使用js"noImplicitAny": false /* 不允许隐式的any类型 */,"forceConsistentCasingInFileNames": true /* 是否强制代码中使用的模块文件名必须和文件系统中的文件名保持大小写一致 */,"baseUrl": ".", //查询的基础路径"paths": { "@/*": ["src/*"], "#/*": ["types/*"] }, //路径映射,配合别名使用"types": [// 编译过程中被包含进来的类型声明文件"node","vite/client"]}
}

tsconfig.json 会有Error提示:找不到“node”的类型定义文件。

3. 配置路径别名

3.1 安装Node.js API的ts类型
pnpm add @types/node -D

对应tsconfig.json文件:

  "types": ["node","vite/client"]

其中 node 表示 “node”的类型声明文件 @types/node 会被加载进来。

3.2 修改vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path' // 引入path模块export default defineConfig({plugins: [vue()],resolve: {// 配置别名alias: {'@': path.resolve('./src'),'#': path.resolve('./types'),},},
})

对应tsconfig.json文件:

"paths": {"@/*": ["src/*"],"#/*": ["types/*"]
}

如果此时 vite.config.ts 文件的 plugins: [vue()] 有报错,重启VScode可解决。

4. 配置 ESLint 和 prettier

4.1 开发工具配置

本文使用代码编辑器为 VSCode,需安装好了相应的插件 Vue - Official(v2.0.10),ESLint 以及 Prettier。

4.2 ESLint 的初始化
pnpm create @eslint/config
√ 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? · typescript
√ Where does your code run? · browser
The config that you've selected requires the following dependencies:eslint, globals, @eslint/js, typescript-eslint, eslint-plugin-vue
√ Would you like to install them now? · No / Yes
√ Which package manager do you want to use? · pnpm

这时候项目根路径下就会生成一份 ESLint 的配置文件 eslint.config.js

import globals from 'globals'
import pluginJs from '@eslint/js'
import tseslint from 'typescript-eslint'
import pluginVue from 'eslint-plugin-vue'export default [{ languageOptions: { globals: globals.browser } },pluginJs.configs.recommended,...tseslint.configs.recommended,...pluginVue.configs['flat/essential'],
]

此时,如果更改VSCode 的配置文件 settings.json

  • 添加如下内容可使用 ESLint 在保存时格式化代码:
	"eslint.experimental.useFlatConfig": true //, 启用 eslint 配置扁平化"editor.formatOnSave": true, // 保存时进行格式化"editor.defaultFormatter": "dbaeumer.vscode-eslint", // 设置 ESLint 为默认格式化工具"eslint.format.enable": true, // 启用 ESLint 格式化下面添加入 validated 的文件"eslint.validate": ["javascript","javascriptreact","typescript","typescriptreact","vue","html","markdown","json","jsonc","yaml"],
4.3 安装 prettier
pnpm install -D eslint-plugin-prettier	eslint-config-prettier

修改 eslint.config.js 添加 prettier 配置

import globals from 'globals'
import pluginJs from '@eslint/js'
import tseslint from 'typescript-eslint'
import pluginVue from 'eslint-plugin-vue'
import pluginPrettierRecommendedConfigs from 'eslint-plugin-prettier/recommended'export default [// eslint 默认推荐规则pluginJs.configs.recommended,// ts 默认推荐规则...tseslint.configs.recommended,// vue3 基础推荐规则...pluginVue.configs['flat/recommended'],// prettier 默认推荐规则pluginPrettierRecommendedConfigs,{languageOptions: { globals: globals.browser },},
]

根目录下新建 prettier.config.js:

export default {tabWidth: 2,useTabs: true,semi: false,singleQuote: true,printWidth: 120,arrowParens: 'always',bracketSpacing: true,endOfLine: 'auto',vueIndentScriptAndStyle: true,
}

这个时候保存时,就会使 eslint 按照 prettier 配置的规则进行格式化,如果有其他错误,重启vscode可解决。

4.4 配置 vue 检测

如果此时打开 components/HelloWorld.vue 文件,会发现此行报错:

defineProps<{ msg: string }>() // Parsing error: Unexpected token )eslint

解决办法: 配置 vue-eslint-parser,修改eslint.config.js

import globals from 'globals'
import pluginJs from '@eslint/js'
import tseslint from 'typescript-eslint'
import pluginVue from 'eslint-plugin-vue'
import pluginPrettierRecommendedConfigs from 'eslint-plugin-prettier/recommended'
import parserVue from 'vue-eslint-parser'export default [// eslint 默认推荐规则pluginJs.configs.recommended,// ts 默认推荐规则...tseslint.configs.recommended,// vue3 基础推荐规则...pluginVue.configs['flat/recommended'],// prettier 默认推荐规则pluginPrettierRecommendedConfigs,{languageOptions: {globals: {...globals.browser,...globals.es2020,...globals.node,},ecmaVersion: 2020,parser: parserVue,parserOptions: {parser: tseslint.parser,},},},// 可添加一些自定义规则rules: {'no-unused-vars': ['off', { caughtErrors: 'none' }], // 未使用变量'@typescript-eslint/no-unused-vars': ['off', { caughtErrors: 'none' }], // 未使用变量'vue/no-unused-vars': ['off', { caughtErrors: 'none' }], // 未使用变量'vue/v-on-event-hyphenation': 'off', // html元素上事件函数名使用短横线连接'vue/multi-word-component-names': ['off'], // 组件名应该多个词组成'vue/require-default-prop': 'warn', // props 参数 应该有默认值'vue/no-v-html': 'off', // 不推荐使用 v-html'vue/no-mutating-props': 'warn', // props 参数应该不能直接修改},
]

至此,保存文件时会按照eslint 和 prettier 的规则进行格式化。

4.4 添加插件 vite-plugin-eslint

安装

pnpm install -D vite-plugin-eslint

修改 vite.config

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import eslintPlugin from 'vite-plugin-eslint'// https://vitejs.dev/config/
export default defineConfig({plugins: [vue(), eslintPlugin()],resolve: {// 配置别名alias: {'@': path.resolve('./src'),'#': path.resolve('./types'),},},
})

添加 vite-plugin-eslint 类型声明

vite-env.d.ts

/// <reference types="vite/client" />/*** 由于 vite-plugin-eslint 库有点落后,导致 vite 高版本不能正确的识别 cjs 模块* 所以这里手动定义*/
declare module 'vite-plugin-eslint' {import { Plugin } from 'vite'import { ESLint } from 'eslint'/** Plugin options, extending from ESlint options */interface Options extends ESLint.Options {/** Path to ESLint instance that will be used for linting */eslintPath?: string/** Check all matching files on project startup */lintOnStart?: boolean/** A single file, or array of files, to include when linting */include?: string | string[]/** A single file, or array of files, to exclude when linting */exclude?: string | string[]/** Custom error formatter or the name of a built-in formatter */formatter?: string | ESLint.Formatter['format']/** The waring found will be printed */emitWarning?: boolean/** The errors found will be printed */emitError?: boolean/** Will cause the module build to fail if there are any warnings, based on emitWarning */failOnWarning?: boolean/** Will cause the module build to fail if there are any errors, based on emitError */failOnError?: boolean}const content: (rawOptions?: Options) => Pluginexport default content
}

5. 添加 element-plus 组件库

5.1 按需导入组件

安装unplugin-vue-components 和 unplugin-auto-import这两款插件

pnpm install element-plus
pnpm install -D unplugin-vue-components unplugin-auto-import

把下列代码插入到 vite.config.ts 配置文件中

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({imports: ['vue'],resolvers: [ElementPlusResolver()],}),Components({resolvers: [ElementPlusResolver()],}),],
})

配置完后使用vue的api和elementPlus的组件时候会自动导入组件以及类型声明。

并且在根目录生成两个文件:auto-imports.d.ts components.d.ts

还需配置tsconfig.json

{..."include": ["src/**/*", "auto-imports.d.ts", "components.d.ts"]
}
5.2 国际化

因为Element Plus 组件 默认 使用英语,所以需要设置成中文

ConfigProvider:

<template><el-config-provider :locale="zhCn" size="large"><app /></el-config-provider>
</template><script lang="ts" setup>import { defineComponent } from 'vue'import { ElConfigProvider } from 'element-plus'import zhCn from 'element-plus/es/locale/lang/zh-cn'
</script>

6. 添加 UnoCSS 和 icons 图标

安装插件:

pnpm install -D unocss  @unocss/preset-rem-to-px @iconify/utils @iconify-json/ep
// vite.config.ts
import UnoCSS from 'unocss/vite'export default defineConfig({plugins: [...UnoCSS()...],
})

创建 uno.config.ts 文件:

// uno.config.ts
import { defineConfig, presetUno, presetAttributify, presetIcons } from 'unocss'
import presetRemToPx from '@unocss/preset-rem-to-px'
import { FileSystemIconLoader } from '@iconify/utils/lib/loader/node-loaders'export default defineConfig({presets: [presetUno(), // 默认样式预设, 此预设继承了 @unocss/preset-wind 和 @unocss/preset-minipresetAttributify(), // 为其他预设和规则提供归因模式。 标签class类名可写为属性presetRemToPx({// 将 rem 转为 px , 1rem = n pxbaseFontSize: 4, // 默认为 16}),presetIcons({collections: {ep: () => import('@iconify-json/ep/icons.json').then((i) => i.default),custom: FileSystemIconLoader('./src/assets/', (svg) => svg.replace(/#FFF/, 'currentColor')),},warn: true,}),],
})

virtual:uno.css 添加到你的主入口中:

// main.ts
import 'virtual:uno.css'

使用示例:

<div text-9xl text-red inline-block i-custom-vue />
<div text-9xl text-black inline-block i-ep-edit />
6.1 浏览器样式重置
pnpm add @unocss/reset
// main.ts
import '@unocss/reset/normalize.css'

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/2659.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【缓存服务】⭐️自定义实现一个简易的数据缓存

目录 &#x1f378;前言 &#x1f37b;手写缓存服务 &#xff08;1&#xff09;缓存实体类 &#xff08;2&#xff09;缓存工具类 &#xff08;3&#xff09;测试缓存服务 &#x1f377;已有的缓存工具 &#x1f379;章末 &#x1f378;前言 俗话说 有轮子不用 就是玩 开个…

WebStorm 中调试 (Debug) JavaScript 文件(js)

WebStorm 中调试 (Debug) JavaScript 文件(js) 在 WebStorm 中调试 JavaScript 文件&#xff0c;您可以设置断点&#xff0c;启动调试会话&#xff0c;并使用浏览器中的开发者工具来查看变量和执行流程。以下是详细步骤&#xff1a; 设置断点 打开您的 JavaScript 文件。 在…

使用 vllm 本地部署 Llama3-8b-Instruct

使用 vllm 本地部署 Llama3-8b-Instruct 0. 引言1. 安装 vllm2. 本地部署 Llama3-8b-Instruct 0. 引言 此文章主要介绍使用 vllm 运行 Llama3-8b。 1. 安装 vllm 创建虚拟环境&#xff0c; conda create -n myvllm python3.11 -y conda activate myvllm安装 Ray 和 Vllm&am…

条件生成对抗网络(cGAN)在AI去衣技术中的应用探索

随着深度学习技术的飞速发展&#xff0c;生成对抗网络&#xff08;GAN&#xff09;作为其中的一个重要分支&#xff0c;在图像生成、图像修复等领域展现出了强大的能力。其中&#xff0c;条件生成对抗网络&#xff08;cGAN&#xff09;通过引入条件变量来控制生成模型的输出&am…

Spring SpringBoot(详解)

1. Spring简介 1.1 Spring 核心设计思想 1.1.1 Spring 是什么&#xff1f; Spring 是包含了众多⼯具⽅法的 IoC 容器。Spring 指的是 Spring Framework&#xff08;Spring 框架&#xff09;&#xff0c;它是⼀个开源框架&#xff0c;Spring ⽀持⼴泛的应⽤场景&#xff0c;它…

SIT3088E:3.0V~5.5V 供电,14Mbps 半双工 RS485/RS422 收发器

特点&#xff1a; 3.0V~5.5V 宽电源范围&#xff0c;半双工&#xff1b;  总线端口 ESD 保护能力 HBM 达到 15kV 以上&#xff1b;  总线容错耐压达到15V&#xff1b;  1/8 单位负载&#xff0c;允许最多 256 个器件连接到总线&#xff1b;  驱动器短路输出保护&…

手撕AVL树(map和set底层结构)(1)

troop主页 今日鸡汤&#xff1a;Action may out always bring happiness;but there is no happiness without action. 行动不一定能带来快乐&#xff0c;但不行动一定不行 C之路还很长 手撕AVL树 一 AVL树概念二 模拟实现AVL树2.1 AVL节点的定义 三 插入更新平衡因子&#xff0…

SpringBoot学习之Kafka下载安装和启动【Mac版本】(三十三)

一、配置Java环境变量 在启动Kafka之前,你需要先正确配置好你的Java环境变量。可以在终端输入java -version检查java环境变量是否配置正确,在Mac上如何配置java环境变量,请读者自行网上搜索操作之,此处不赘叙。 二、下载安装Kafka 1、下载Kafka:Apache Kafka,这两个版本…

四川赢涟电子商务有限公司深耕抖音电商服务

在当今数字化、网络化高速发展的时代&#xff0c;电子商务行业异军突起&#xff0c;成为推动经济增长的重要力量。四川赢涟电子商务有限公司凭借其敏锐的市场洞察力和创新精神&#xff0c;专注于抖音电商服务&#xff0c;致力于为广大消费者提供便捷、高效、个性化的购物体验&a…

Paddle 1.8 与 Paddle 2.0 API 映射表

安装2.6的paddlepaddle之后总是报fluid的错误&#xff0c;查询得知这个接口已经弃用了&#xff0c;但是一直找不到替换接口&#xff0c;偶然查询报错信息的时候找到了映射表&#xff0c;转存一下。 Paddle 1.8 与 Paddle 2.0 API 映射表

Debian 12.5(代号 “Bookworm“)中安装中文支持

在 Debian 12.5&#xff08;代号 "Bookworm"&#xff09;中安装中文支持通常涉及以下几个步骤&#xff1a; 1. **选择语言**&#xff1a; 在安装过程中&#xff0c;安装程序会询问您选择界面语言&#xff0c;您可以选择中文。 2. **安装中文语言包**&#xff1a…

如何优雅地Spring事务编程

本文已收录至Github&#xff0c;推荐阅读 &#x1f449; Java随想录 微信公众号&#xff1a;Java随想录 在开发中&#xff0c;有时候我们需要对 Spring 事务的生命周期进行监控&#xff0c;比如在事务提交、回滚或挂起时触发特定的逻辑处理。那么如何实现这种定制化操作呢&am…

直播报名 | 科技出海新势力,遥感+AI助力一带一路

遥感技术的出海之路顺畅吗&#xff1f; 国内外遥感应用的关注点相同吗&#xff1f; 目前珈和主要辐射哪些海外国家&#xff1f; … 上周数据赋农季第三期《科技出海&#xff0c;遥感AI赋能“一带一路”提升种植园规模效益》直播预告一出&#xff0c;小伙伴们纷纷来咨询珈和的海…

CentOS安装htop工具

启用 EPEL Repository 安装Htop 首先启用 EPEL Repository: yum -y install epel-release启用 EPEL Repository 后, 可以用 yum 直接安裝 Htop: 安装htop yum -y install htop安装成功 输入htop使用工具 htop安装glances工具 yum install glances

汉译英早操练-(十六)

hello大家好&#xff0c;又到了一周一度的汉译英早操练时间。今天是周三你感觉怎么样&#xff1f;什么&#xff1f;你不想学英语&#xff0c;来来来 操练起来&#xff0c;今天是一个小练习&#xff0c;讲西藏的一个小篇幅说明文&#xff0c;虽然老生常谈&#xff0c;但谁说简…

达梦(DM)数据库管理表

达梦DM数据库管理表 管理表管理表的准则表的存储空间上限表的存储位置 创建表创建普通表指定表的聚集索引查询建表更改表删除表清空表查看表定义查看表空间使用 表是数据库中数据存储的基本单元&#xff0c;是对用户数据进行读和操纵的逻辑实体&#xff0c;表由列和行组成&…

Vue的列表渲染和双向绑定(初学vue之v-for以及v-model)

目录 一、列表渲染&#xff08;v-for&#xff09;介绍&#xff1a; 1.概念 2.好处 3.作用 4.应用 5.用法 二、双向绑定&#xff08;v-model&#xff09;介绍&#xff1a; 1.概念 2.好处 3.作用 4.应用 5.用法 三、v-for和v-model具体用法 1.v-for的基本使用 2.v…

Springboot+Vue项目-基于Java+MySQL的企业客户管理系统(附源码+演示视频+LW)

大家好&#xff01;我是程序猿老A&#xff0c;感谢您阅读本文&#xff0c;欢迎一键三连哦。 &#x1f49e;当前专栏&#xff1a;Java毕业设计 精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; &#x1f380; Python毕业设计 &…

前端零代码开发实践:页面嵌套+逻辑连线0开发扩展组件,实现切换开关控制扇叶转动。能无代码封装扩展组件,有别于常规的web组态或低代码平台

前言&#xff1a; 官网:http://www.uiotos.net/ 什么是 UIOTOS&#xff1f; 这是一款拥有独创专利技术的前端零代码工具&#xff0c;专注于解决前端界面开发定制难题&#xff0c;原型即应用&#xff01;具有页面嵌套、属性继承、节点连线等全新特性&#xff0c;学习门槛低…

网络安全之SQL注入及防御(下篇)

目录 什么是SQL注入&#xff1f; 一&#xff0c;SQL注入会导致什么呢&#xff1f; 二&#xff0c;SQL注入思想与步骤 三&#xff0c;SQL注入的绕过 四&#xff0c;sqlmap工具的使用 五&#xff0c;sql注入的防御方法 总结 什么是SQL注入&#xff1f; SQL注入&#xff08;…