vue3+ts+pinia+vant项目搭建详细步骤 |
1.pnpm介绍
安装: 2.基础创建2.1 创建项目创建 2.2 目录调整及介绍./src ├── assets `静态资源,图片...` ├── components `通用组件` ├── router `路由` │ └── index.ts ├── api `接口服务API` ├── stores `状态仓库` ├── styles `样式` │ └── main.scss ├── types `TS类型` ├── utils `工具函数` ├── views `页面` ├── main.ts `入口文件` └──App.vue `根组件` 2.3 env.d.ts可以看到 // 声明文件 // 用于引入 Vite 提供的类型声明,使 TypeScript 了解 ImportMeta 和 ImportMetaEnv 的类型 /// <reference types="vite/client" /> declare module '*.vue' { import type { DefineComponent } from 'vue' const component: DefineComponent<{}, {}, any> export default component } 2.4 tsconfig.json
{ "compilerOptions": { "target": "ESNext", // 目标转化的语法 "useDefineForClassFields": true, "module": "ESNext", // 转化的格式 "moduleResolution": "Node", //解析规则 "strict": true, //严格模式 "sourceMap": true, // 启动sourceMap调试 "jsx": "preserve", // 不允许ts编译jsx语法 "resolveJsonModule": true, "isolatedModules": true, "esModuleInterop": true, //es6和commonjs转化 "lib": [ "ESNext", "DOM" ], "skipLibCheck": true, "noEmit": true, "baseUrl": ".", "paths": { "@/*": [ "src/*" ], "components": [ "src/components/*" ], "_pinia/*": [ "src/pinia/*" ] }, }, "include": [ "src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", "types" ], } 2.5 eslint配置2.5.1 安装额外依赖
2.5.2 配置.eslintrc.cjs文件module.exports = { env: { //环境 针对环境的语法 browser: true, es2021: true, node: true }, // 集成了哪些规则 别人写好的 extends: ['eslint:recommended', 'plugin:vue/vue3-essential', 'plugin:@typescript-eslint/recommended'], overrides: [], // 'parser': '@typescript-eslint/parser', // 可以解析.vue 文件 parser: 'vue-eslint-parser', parserOptions: { ecmaVersion: 'latest', sourceType: 'module', parser: '@typescript-eslint/parser' // 解析ts文件 }, plugins: ['vue', '@typescript-eslint', 'prettier'], // 自定义的规则 rules: { 'vue/multi-word-component-names': 'off', 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', 'arrow-spacing': [ 2, { //强制箭头函数前后都使用空格 before: true, after: true } ], 'prettier/prettier': 'off', "@typescript-eslint/no-explicit-any": ["off"], // 关闭不能定义any类型的校验 'no-irregular-whitespace': 2, // 不能有不规则的空格 'comma-dangle': [2, 'never'] // 对象字面量项尾不能有逗号 } } 2.5.3 配置.eslintignore文件.DS_Store node_modules /dist # local env files .env.local .env.*.local # Log files npm-debug.log* yarn-debug.log* yarn-error.log* pnpm-debug.log* # Editor directories and files .idea .vscode *.suo *.ntvs* *.njsproj *.sln *.sw? 2.5.4 校验命令可以通过执行 2.6 prettier配置2.6.1 创建.eslintrc.cjsmodule.exports = { printWidth: 200, //一行内容的宽度,默认80 singleQuote: true, //使用双引号 semi: false, // 末尾添加分号 tabWidth: 2, trailingComma: 'none', useTabs: false, endOfLine: 'auto' } 2.6.2 取消勾选2.6.3 勾选保存2.7 配置代码检查工作流-husky提交代码前做代码检查 ,使用husky这个git hooks工具
2.8 commitlint用于提交
#!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" npx --no-install commitlint --edit $1
module.exports={extends:['@commitlint/config-conventional'],rules:{}} 3.Vant官网
import 'vant/lib/index.css' import vant from 'vant' app.use(vant)
// Toast import { showToast } from 'vant'; import 'vant/es/toast/style'; // Dialog import { showDialog } from 'vant'; import 'vant/es/dialog/style'; // Notify import { showNotify } from 'vant'; import 'vant/es/notify/style'; // ImagePreview import { showImagePreview } from 'vant'; import 'vant/es/image-preview/style'; 4.移动端适配安装: // eslint-disable-next-line no-undef module.exports = { plugins: { 'postcss-px-to-viewport': { // 设备宽度375计算vw的值 viewportWidth: 375, }, }, }; 5.unplugin-auto-import 自动引入实现依赖的自动导入,不用再频繁导入依赖包,
// vite.config.js import AutoImport from 'unplugin-auto-import/vite' export default defineConfig({ plugins: [ vue(), AutoImport({ imports: ['vue', 'vue-router'] // eslintrc: { enabled: true } }) ], ... })
总结到此这篇关于vue3+ts+pinia+vant项目搭建的文章就介绍到这了,更多相关vue3+ts+pinia+vant项目搭建内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持! |