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

相关文章

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

springboot项目如何开启https服务

《springboot项目如何开启https服务》:本文主要介绍springboot项目如何开启https服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录springboot项目开启https服务1. 生成SSL证书密钥库使用keytool生成自签名证书将

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

将Java项目提交到云服务器的流程步骤

《将Java项目提交到云服务器的流程步骤》所谓将项目提交到云服务器即将你的项目打成一个jar包然后提交到云服务器即可,因此我们需要准备服务器环境为:Linux+JDK+MariDB(MySQL)+Gi... 目录1. 安装 jdk1.1 查看 jdk 版本1.2 下载 jdk2. 安装 mariadb(my

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行