Vue2项目搭建:Vue2.7+Vite4+Pinia+TailwindCSS+Prettier+ESLint

本文主要是介绍Vue2项目搭建:Vue2.7+Vite4+Pinia+TailwindCSS+Prettier+ESLint,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目前 create-vue 和 Vite 都不提供 Vue2 项目的搭建,不想用 Vue CLI 和 webpack,于是就打算从 0 搭建一个工程化项目,支持组合式 API (Composition API) 写法,没有使用 TypeScript,有朋友需要的话我可以再完善一下。

  • Node.js 16.x
  • pnpm 8.x

初始化

mkdir vue2-vite
cd vue2-vite
pnpm init

安装依赖:

pnpm install vue@^2 vue-router@^3 pinia
pnpm install vite@^4 @vitejs/plugin-vue2 tailwindcss postcss autoprefixer prettier prettier-plugin-tailwindcss eslint@^8 eslint-config-prettier eslint-plugin-prettier eslint-plugin-vue -D

package.json:

{"name": "vue2-vite","version": "1.0.0","scripts": {"dev": "vite","build": "vite build","preview": "vite preview","lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore","format": "prettier --write \"src/**/*.{vue,js,css,scss}\""},"keywords": [],"author": "","license": "ISC","devDependencies": {"@vitejs/plugin-vue2": "^2.3.1","autoprefixer": "^10.4.20","eslint": "^8.57.0","eslint-config-prettier": "^9.1.0","eslint-plugin-prettier": "^5.2.1","eslint-plugin-vue": "^9.27.0","postcss": "^8.4.41","prettier": "^3.3.3","prettier-plugin-tailwindcss": "^0.6.6","tailwindcss": "^3.4.10","vite": "^4.5.3"},"dependencies": {"pinia": "^2.2.2","vue": "^2.7.16","vue-router": "^3.6.5"}
}

Vite

新建 vite.config.js 文件:

import vue from '@vitejs/plugin-vue2'
import { fileURLToPath, URL } from 'node:url'export default {plugins: [vue()],resolve: {alias: {'@': fileURLToPath(new URL('./src', import.meta.url)),},},
}

Tailwind CSS

pnpm tailwindcss init -p

修改 tailwindcss.config.js 文件:

/** @type {import('tailwindcss').Config} */
export default {content: ["./index.html","./src/**/*.{js,ts,jsx,tsx,vue}",],theme: {extend: {},},plugins: [],
}

新建 src/assets/styles/index.css 文件:

@tailwind base;  
@tailwind components;  
@tailwind utilities;

Prettier

新建 prettier.config.mjs 文件:

/*** @see https://prettier.io/docs/en/configuration.html* @type {import("prettier").Config}*/
export default {semi: false,singleQuote: true,htmlWhitespaceSensitivity: 'ignore',plugins: ['prettier-plugin-tailwindcss'],
}

ESLint

新建 .eslintrc.cjs 文件:

/* eslint-env node */  
module.exports = {  root: true,  extends: ['plugin:vue/recommended', 'eslint:recommended', 'prettier'],  plugins: ['prettier'],  rules: {  'vue/multi-word-component-names': 'off',  },  
}

Vue

新建 index.html文件:

<!doctype html>
<html lang="zh"><head><meta charset="UTF-8" /><link rel="icon" href="/favicon.ico"><metaname="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/><meta http-equiv="X-UA-Compatible" content="ie=edge" /><title>vue2-vite</title></head><body><div id="app"></div><script type="module" src="/src/main.js"></script></body>
</html>

新建入口文件 src/main.js

import Vue from 'vue'
import { createPinia, PiniaVuePlugin } from 'pinia'import App from '@/App.vue'
import router from '@/router'
import '@/assets/styles/index.css'const pinia = createPinia()Vue.use(PiniaVuePlugin)new Vue({render: (h) => h(App),router,pinia,
}).$mount('#app')

新建 src/App.vue 文件:

<script setup></script><template><div><router-view /></div>
</template>

tips:如果是 WebStorm 编辑器,并且是通过 pnpm 安装的依赖,可能会遇到 router-view、router-link 标签无法识别的问题,可以展开 node_modules 文件夹,找到 vue-router,右键,将目标标记为 -> 不排除,就可以了。类似问题:https://youtrack.jetbrains.com/issue/WEB-56972/Vue-library-components-not-resolved-when-installed-with-pnpm

Vue Router

新建 src/router/index.js 文件:

import Vue from 'vue'
import Router from 'vue-router'Vue.use(Router)const routes = [{path: '/',name: 'home',component: () => import('@/views/Home.vue'),},
]const router = new Router({mode: 'history',routes,
})export default router

新建 src/views/Home.vue 文件:

<script setup></script><template><div>Home</div>
</template>

pinia

新建 src/store/counter.js

import { computed, ref } from 'vue'  
import { defineStore } from 'pinia'  export const useCounterStore = defineStore('counter', () => {  const count = ref(0)  const doubleCount = computed(() => count.value * 2)  function increment() {  count.value++  }  return { count, doubleCount, increment }  
})

在 Vue 中使用:

<script setup>
import { useCounterStore } from '@/store/counter'const counterStore = useCounterStore()
</script><template><div><div>{{ counterStore.count }}</div><div>{{ counterStore.doubleCount }}</div><button @click="counterStore.increment">increment</button></div>
</template>

这篇关于Vue2项目搭建:Vue2.7+Vite4+Pinia+TailwindCSS+Prettier+ESLint的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1138298

相关文章

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加

tomcat多实例部署的项目实践

《tomcat多实例部署的项目实践》Tomcat多实例是指在一台设备上运行多个Tomcat服务,这些Tomcat相互独立,本文主要介绍了tomcat多实例部署的项目实践,具有一定的参考价值,感兴趣的可... 目录1.创建项目目录,测试文China编程件2js.创建实例的安装目录3.准备实例的配置文件4.编辑实例的

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

css中的 vertical-align与line-height作用详解

《css中的vertical-align与line-height作用详解》:本文主要介绍了CSS中的`vertical-align`和`line-height`属性,包括它们的作用、适用元素、属性值、常见使用场景、常见问题及解决方案,详细内容请阅读本文,希望能对你有所帮助... 目录vertical-ali

springboot集成Deepseek4j的项目实践

《springboot集成Deepseek4j的项目实践》本文主要介绍了springboot集成Deepseek4j的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录Deepseek4j快速开始Maven 依js赖基础配置基础使用示例1. 流式返回示例2. 进阶

SpringBoot项目启动报错"找不到或无法加载主类"的解决方法

《SpringBoot项目启动报错找不到或无法加载主类的解决方法》在使用IntelliJIDEA开发基于SpringBoot框架的Java程序时,可能会出现找不到或无法加载主类com.example.... 目录一、问题描述二、排查过程三、解决方案一、问题描述在使用 IntelliJ IDEA 开发基于

SpringBoot项目使用MDC给日志增加唯一标识的实现步骤

《SpringBoot项目使用MDC给日志增加唯一标识的实现步骤》本文介绍了如何在SpringBoot项目中使用MDC(MappedDiagnosticContext)为日志增加唯一标识,以便于日... 目录【Java】SpringBoot项目使用MDC给日志增加唯一标识,方便日志追踪1.日志效果2.实现步

浅析CSS 中z - index属性的作用及在什么情况下会失效

《浅析CSS中z-index属性的作用及在什么情况下会失效》z-index属性用于控制元素的堆叠顺序,值越大,元素越显示在上层,它需要元素具有定位属性(如relative、absolute、fi... 目录1. z-index 属性的作用2. z-index 失效的情况2.1 元素没有定位属性2.2 元素处

Python实现html转png的完美方案介绍

《Python实现html转png的完美方案介绍》这篇文章主要为大家详细介绍了如何使用Python实现html转png功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 1.增强稳定性与错误处理建议使用三层异常捕获结构:try: with sync_playwright(

Vue 调用摄像头扫描条码功能实现代码

《Vue调用摄像头扫描条码功能实现代码》本文介绍了如何使用Vue.js和jsQR库来实现调用摄像头并扫描条码的功能,通过安装依赖、获取摄像头视频流、解析条码等步骤,实现了从开始扫描到停止扫描的完整流... 目录实现步骤:代码实现1. 安装依赖2. vue 页面代码功能说明注意事项以下是一个基于 Vue.js