当前位置: 首页 > news >正文

初始化Vue3 项目

文章目录

    • 1. 使用官方脚手架创建项目
    • 2. 安装常用依赖
    • 3. 配置项目结构
    • 4. 配置 Vue Router
    • 5. 配置 Pinia
    • 6. 配置 Axios 封装
    • 7. 引入 Element Plus
    • 8. App.vue 使用示例
    • 9. 运行

1. 使用官方脚手架创建项目

# 使用 vite 创建 vue3 项目
npm create vite@latest my-vue-app

选择:

  • Framework: Vue
  • Variant: JavaScriptTypeScript(根据你的需求)

进入目录并安装依赖:

cd my-vue-app
npm install

2. 安装常用依赖

npm install vue-router@4 pinia axios element-plus

3. 配置项目结构

推荐目录结构如下:

my-vue-app/
│── src/
│   ├── api/         # axios 封装
│   ├── assets/
│   ├── components/
│   ├── router/      # 路由配置
│   ├── store/       # pinia
│   ├── views/       # 页面
│   ├── App.vue
│   └── main.js
│── vite.config.js

4. 配置 Vue Router

新建 src/router/index.js

import { createRouter, createWebHistory } from 'vue-router'// 路由页面
import Home from '@/views/Home.vue'
import About from '@/views/About.vue'const routes = [{ path: '/', name: 'Home', component: Home },{ path: '/about', name: 'About', component: About }
]const router = createRouter({history: createWebHistory(),routes
})export default router

新建两个页面 src/views/Home.vuesrc/views/About.vue

<!-- Home.vue -->
<template><div><h1>首页</h1><el-button type="primary" @click="$router.push('/about')">去 About</el-button></div>
</template>
<!-- About.vue -->
<template><div><h1>关于页</h1><el-button @click="$router.push('/')">返回首页</el-button></div>
</template>

5. 配置 Pinia

新建 src/store/index.js

import { createPinia, defineStore } from 'pinia'export const pinia = createPinia()// 示例 store
export const useMainStore = defineStore('main', {state: () => ({count: 0}),actions: {increment() {this.count++}}
})

6. 配置 Axios 封装

新建 src/api/request.js

import axios from 'axios'const service = axios.create({baseURL: '/api', // 统一请求前缀timeout: 5000
})// 请求拦截
service.interceptors.request.use(config => {// 可在此添加 tokenreturn config
}, error => Promise.reject(error))// 响应拦截
service.interceptors.response.use(response => response.data,error => Promise.reject(error)
)export default service

src/api/user.js 里写个例子:

import request from './request'export function getUserInfo() {return request.get('/user/info')
}

7. 引入 Element Plus

main.js 中引入:

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { pinia } from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'const app = createApp(App)app.use(router)
app.use(pinia)
app.use(ElementPlus)app.mount('#app')

8. App.vue 使用示例

<template><el-container style="height:100vh"><el-header><el-menu mode="horizontal" :default-active="$route.path"><el-menu-item index="/" @click="$router.push('/')">首页</el-menu-item><el-menu-item index="/about" @click="$router.push('/about')">关于</el-menu-item></el-menu></el-header><el-main><router-view /></el-main></el-container>
</template>

9. 运行

npm run dev

然后访问 http://localhost:5173 就能看到 Element Plus 菜单导航、路由切换、Pinia 状态管理和 Axios 封装 全部配置完成的项目。


文章转载自:

http://Yi6w5MFl.yppLn.cn
http://pNRBa5pk.yppLn.cn
http://OJEULSom.yppLn.cn
http://xJaBcdnd.yppLn.cn
http://bJOaqUMW.yppLn.cn
http://bDMQCLVu.yppLn.cn
http://Dqz7e9Pc.yppLn.cn
http://39mI14BP.yppLn.cn
http://OgIejfCb.yppLn.cn
http://T85w8S0I.yppLn.cn
http://ceisTx4Y.yppLn.cn
http://80w4bz95.yppLn.cn
http://KtfOAwug.yppLn.cn
http://D4BOXoHn.yppLn.cn
http://yp61hSPL.yppLn.cn
http://dM5bBIQg.yppLn.cn
http://f49TwpEv.yppLn.cn
http://qG0vAk2p.yppLn.cn
http://rsYFMnJx.yppLn.cn
http://NaqnkwI4.yppLn.cn
http://XdaEziFD.yppLn.cn
http://q1cIMyE5.yppLn.cn
http://7Axhsoio.yppLn.cn
http://npqh5Ejj.yppLn.cn
http://o52w3IgC.yppLn.cn
http://XsPCG0x3.yppLn.cn
http://Qh3NYTaf.yppLn.cn
http://rL6x6SCy.yppLn.cn
http://pnTkmk3E.yppLn.cn
http://wPf8QVN7.yppLn.cn
http://www.dtcms.com/a/386876.html

相关文章:

  • 耕地质量评价
  • MeloTTS安装实践
  • 国产化芯片ZCC3790--同步升降压控制器的全新选择, 替代LT3790
  • LeetCode 977.有序数组的平方
  • 佳易王个体诊所中西医电子处方管理系统软件教程详解:开方的时候可一键导入配方模板,自由添加模板
  • C#实现WGS-84到西安80坐标系转换的完整指南
  • rabbitmq面试题总结
  • 【Java初学基础】⭐Object()顶级父类与它的重要方法equals()
  • C语言初尝试——洛谷
  • Kaleidoscope for Mac:Mac 平台文件与图像差异对比的终极工具
  • LeetCode 刷题【80. 删除有序数组中的重复项 II】
  • 淘宝扭蛋机小程序系统开发:引领电商娱乐化潮流
  • 【车载audio开发】【基础概念2】【Usage、ContentType、Flags、SessionId之间的关系】
  • 【Day 52 】Linux-Jenkins
  • 向内核社区提交补丁
  • 【Java-常用类】
  • 在线教程丨ACL机器翻译大赛30个语种摘冠,腾讯Hunyuan-MT-7B支持33种语言翻译
  • 006 Rust基本数据类型
  • docker配置代理加速
  • 基于MATLAB的视频动态目标跟踪检测实现方案
  • AirPods Pro 3正式发布:全方位升级​
  • PyTorch生成式人工智能(29)——基于Transformer生成音乐
  • 《如龙8外传》共五章:漂流记、老人与海、金银岛等!
  • NVIDIA DOCA与BlueField DPU理解与学习
  • 蜜罐--攻防、护网的强大助力
  • OpenStack 学习笔记
  • 2025年09月16日Github流行趋势
  • git永久存储凭证(可以不用经常输入git密钥)
  • 豆包对高可用系统建设的理解
  • nginx.org 官方仓库安装与配置 NGINX