Vue2项目搭建:Vue2.7+Vite4+Pinia+TailwindCSS+Prettier+ESLint
目前 create-vue 和 Vite 都不提供 Vue2 项目的搭建,不想用 Vue CLI 和 webpack,于是就打算从 0 搭建一个工程化项目,支持组合式 API (Composition API) 写法,没有使用 TypeScript,有朋友需要的话我可以再完善一下。
目前 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">
<meta
name="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>
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)