从零开始搭建 Vue 3+Vite+TypeScript+Pinia+Vue Router+SCSS+StyleLint+CommitLint+...项目

结合 Vue 3、Vite、TypeScript、Pinia、Vue Router、SCSS、StyleLint、CommitLint、Husky 和 Lint-Staged,让你的开发体验更加顺畅!💻✨

目录

  1. 环境准备
  2. 创建项目
  3. 安装依赖
  4. 配置 TypeScript
  5. 设置 Vue Router
  6. 使用 Pinia 状态管理
  7. SCSS 配置
  8. ESLint 配置
  9. 代码风格检查与提交规范
  10. 总结

环境准备

在开始之前,请确保你的开发环境中已安装以下工具:

  • Node.js (建议使用 LTS 版本)
  • npm 或 yarn

你可以通过以下命令检查 Node.js 和 npm 的版本:

node -v
npm -v

创建项目

首先,我们使用 Vite 创建一个新的 Vue 3 + ts 项目。打开终端,执行以下命令:

npm create vite@latest my-vue-app --template vue-ts

进入项目目录:

cd my-vue-app

安装依赖

接下来,我们需要安装一些必要的依赖。执行以下命令安装核心依赖:

npm install pinia vue-router
npm install pinia vue-router

然后,为了在开发环境中使用样式和代码风格检查工具,我们需要安装以下开发依赖:

npm install --save-dev sass stylelint stylelint-config-standard husky commitlint @commitlint/config-conventional lint-staged eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin

配置 TypeScript

Vite 默认会为我们配置 TypeScript,但我们可以根据需要进行一些调整。打开 tsconfig.json 文件,确保以下配置存在:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "strict": true,
    "jsx": "preserve",
    "moduleResolution": "Node",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
  "exclude": ["node_modules"]
}

设置 Vue Router

在 src 目录下创建一个 router 文件夹,并在其中创建 index.ts 文件:

import { createRouter, createWebHistory } from 'vue-router';

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home.vue'),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

export default router;

然后在 main.ts 中引入并使用 Vue Router:

import { createApp } from 'vue';
import App from './App.vue';
import router from './router';

createApp(App).use(router).mount('#app');

使用 Pinia 状态管理

在 src 目录下创建一个 stores 文件夹,并在其中创建 index.ts 文件:

import { createPinia } from 'pinia';

const pinia = createPinia();

export default pinia;

然后在 main.ts 中引入并使用 Pinia:

import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import pinia from './stores';

createApp(App).use(router).use(pinia).mount('#app');

SCSS 配置

在 src 目录下创建一个 styles 文件夹,并在其中创建 main.scss 文件:

$primary-color: #42b983;

body {
  font-family: Arial, sans-serif;
  background-color: $primary-color;
}

 在 main.ts 中引入 SCSS 文件:

import './styles/main.scss';

ESLint 配置

在项目根目录下创建 .eslintrc.js 文件,添加以下内容:

module.exports = {
  root: true,
  env: {
    node: true,
    browser: true,
    es2021: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:vue/vue3-recommended',
    '@vue/typescript/recommended',
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 2021,
    sourceType: 'module',
  },
  rules: {
    'vue/multi-word-component-names': 'off', // 关闭组件名称多词规则
    'indent': ['error', 2], // 强制使用2个空格缩进
    'quotes': ['error', 'single'], // 强制使用单引号
  },
};

代码风格检查与提交规范

StyleLint

在项目根目录下创建 .stylelintrc.json 文件,添加以下内容:

{
  "extends": "stylelint-config-standard",
  "rules": {
    "indentation": 2,
    "string-quotes": "single"
  }
}

CommitLint

在项目根目录下创建 commitlint.config.js 文件,添加以下内容:

module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'header-max-length': [2, 'always', 72], // 限制提交信息的最大长度为72个字符
    'type-case': [2, 'always', ['lower-case', 'upper-case']],
    'type-enum': [
      2,
      'always',
      [
        'feat',
        'fix',
        'docs',
        'style',
        'refactor',
        'merge',
        'test',
        'chore',
        'revert',
        'build',
        'ci',
        'perf'
      ]

    ],
  }
};

Husky 和 Lint-Staged

初始化 Husky:

npx husky install

添加 Git 提交钩子:

npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'

接下来,配置 Lint-Staged。在项目根目录下创建 lint-staged.config.js 文件,添加以下内容(可以创建配置文件或者直接在package.json配置 二选一):

module.exports = {
  '*.{js,jsx,ts,tsx,vue}': 'eslint --fix',
  '*.scss': 'stylelint --fix',
};

在 package.json 中配置 Lint-Staged

在 package.json 文件中添加以下内容,以便将 Lint-Staged 集成到 Husky 的 pre-commit 钩子中:

{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
   "src/**/*.{js,ts,tsx,jsx,vue}": [
      "eslint --fix",
      "prettier --write",
      "git add"
    ],
    "package.json": [
      "prettier --write",
      "git add"
    ],
    "*.md": [
      "prettier --write",
      "git add"
    ],
    "*.scss": "stylelint --fix"
  }
}

 

总结

恭喜你!🎉 现在你已经成功搭建了一个基于 Vue 3、Vite、TypeScript、Pinia、Vue Router、SCSS、StyleLint、CommitLint、Husky 和 Lint-Staged 的开发环境。

希望这个教程对你有所帮助!如果你有任何问题或建议,请在评论区留言。让我们一起进步!💪

原文:https://juejin.cn/post/7451485514596745243

对于使用 Vite + Vue3 + TypeScript + Pinia + Vue Router + Axios + SCSS 并自动导入 API 的设置,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了 Node.js,并且版本大于等于 12.0.0。 2. 创建一个新的 Vue 项目,可以使用 Vue CLI 或者手动创建一个空文件夹。 3.项目根目录下,打开终端并执行以下命令安装 Vite: ```bash npm init vite@latest ``` 按照提示选择你的项目配置,包括选择 Vue 3TypeScript 和其他选项。 4. 进入项目目录并安装依赖: ```bash cd your-project-name npm install ``` 5. 安装 Pinia 插件: ```bash npm install pinia ``` 6. 创建一个 `src/store` 目录,并在其中创建 `index.ts` 文件,用于定义和导出你的 Pinia store。 ```typescript // src/store/index.ts import { createPinia } from 'pinia' export const store = createPinia() // 可以在这里定义你的 store 模块 ``` 7.项目根目录下创建 `src/api` 目录,用于存放 API 请求相关的文件。 8. 在 `src/api` 目录下创建一个 `index.ts` 文件,用于自动导入所有 API 文件。 ```typescript // src/api/index.ts const modules = import.meta.globEager('./*.ts') const apis: any = {} for (const path in modules) { if (path !== './index.ts') { const moduleName = path.replace(/^.\/|\.ts$/g, '') apis[moduleName] = modules[path].default } } export default apis ``` 这样,你就可以在 `src/api` 目录下创建各种 API 请求的文件,例如 `user.ts`: ```typescript // src/api/user.ts import axios from 'axios' export function getUser(id: number) { return axios.get(`/api/user/${id}`) } ``` 然后,在你的组件中使用自动导入的 API: ```typescript import { defineComponent, ref } from 'vue' import { useUserStore } from '@/store' import apis from '@/api' export default defineComponent({ setup() { const userStore = useUserStore() const userId = ref(1) const fetchUser = async () => { const response = await apis.user.getUser(userId.value) userStore.setUser(response.data) } return { userId, fetchUser, } }, }) ``` 以上就是使用 Vite + Vue3 + TypeScript + Pinia + Vue Router + Axios + SCSS 并自动导入 API 的基本设置。你可以根据自己的需求进一步配置和扩展。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值