您好,登录后才能下订单哦!
# 安装Vue.js 3的步骤
Vue.js 3作为当前最流行的前端框架之一,以其轻量级、高性能和渐进式设计受到开发者青睐。本文将详细介绍Vue.js 3的多种安装方式及配套工具链配置,涵盖从基础环境准备到项目创建的完整流程。
## 一、环境准备
### 1. Node.js安装
Vue.js 3的运行依赖Node.js环境,建议安装LTS版本(当前推荐18.x+):
```bash
# 在Windows/macOS上:
1. 访问官网 https://nodejs.org 下载安装包
2. 运行安装向导(建议勾选npm package manager)
3. 验证安装:
node -v # 应显示版本号如v18.16.0
npm -v # 应显示版本号如9.5.1
# Linux用户(Ubuntu示例):
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
推荐使用更快的pnpm或yarn:
# 安装pnpm
npm install -g pnpm
# 或安装yarn
npm install -g yarn
适合学习或简单demo:
<!-- 开发环境版本 -->
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<!-- 生产环境版本 -->
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
使用示例:
<div id="app">{{ message }}</div>
<script>
const { createApp } = Vue
createApp({
data() {
return { message: 'Hello Vue!' }
}
}).mount('#app')
</script>
Vite是Vue官方推荐的构建工具:
# 使用npm
npm create vue@latest
# 使用pnpm
pnpm create vue@latest
# 使用yarn
yarn create vue@latest
交互式命令行将引导你: 1. 输入项目名称 2. 选择需要集成的功能(TypeScript、JSX、Router、Pinia等) 3. 完成创建后:
cd your-project-name
npm install
npm run dev
虽然Vue CLI仍支持,但已进入维护模式:
npm install -g @vue/cli
vue create my-project
# 选择Vue 3预设
典型Vite+Vue3项目结构:
my-project/
├── public/ # 静态资源
├── src/
│ ├── assets/ # 模块资源
│ ├── components/ # 组件
│ ├── App.vue # 根组件
│ └── main.js # 入口文件
├── index.html # 主页面
├── vite.config.js # Vite配置
└── package.json
确保编辑器安装Volar扩展(替代Vetur): - VS Code搜索安装”Vue Language Features (Volar)” - 禁用Vetur(如果已安装)
安装Vue Router 4:
npm install vue-router@4
配置示例:
// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
const routes = [
{ path: '/', component: () => import('../views/Home.vue') }
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
安装推荐的状态库:
npm install pinia
使用示例:
// src/stores/counter.js
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('counter', {
state: () => ({ count: 0 }),
actions: {
increment() {
this.count++
}
}
})
npm run dev
npm run build
生成的文件位于dist/
目录
npm run preview
版本兼容问题:
ESLint配置:
npm install eslint eslint-plugin-vue --save-dev
配置.eslintrc.js
:
module.exports = {
extends: ['plugin:vue/vue3-recommended']
}
浏览器兼容:
在vite.config.js
中添加:
“`javascript
import legacy from ‘@vitejs/plugin-legacy’
export default { plugins: [ legacy({ targets: [‘defaults’, ‘not IE 11’] }) ] }
## 七、TypeScript支持
1. 创建时选择TypeScript模板,或手动添加:
```bash
npm install -D typescript @vue/compiler-sfc
tsconfig.json
:{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"strict": true,
"jsx": "preserve",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
}
npm install -D vitest @vue/test-utils jsdom
配置vite.config.js
:
/// <reference types="vitest" />
export default {
test: {
globals: true,
environment: 'jsdom'
}
}
.github/workflows/ci.yml
示例:
name: CI
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm ci
- run: npm run test:unit
通过以上步骤,您已经完成了Vue.js 3开发环境的完整配置。建议定期更新依赖保持版本最新:
npm outdated
npm update
更多高级特性可参考官方文档。Happy coding! “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。