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

Vue3+Vite MPA多页面应用开发完整指南 – 从零搭建到部署优化

什么是 MPA 多页面应用

MPA(Multi-Page Application)是由多个独立的 HTML 页面组成的应用,每个页面都有独立的入口文件。与 SPA 不同,MPA 的每个页面都是独立的,页面间通过链接跳转,适合大型项目或需要 SEO 优化的场景。

项目结构设计

vue3-vite-mpa/
├── src/
│   ├── pages/
│   │   ├── home/
│   │   │   ├── index.html
│   │   │   ├── main.js
│   │   │   └── App.vue
│   │   ├── about/
│   │   │   ├── index.html
│   │   │   ├── main.js
│   │   │   └── App.vue
│   │   └── admin/
│   │       ├── index.html
│   │       ├── main.js
│   │       └── App.vue
│   ├── components/
│   │   ├── Header.vue
│   │   └── Footer.vue
│   └── assets/
├── package.json
└── vite.config.js

基础配置

安装依赖

npm create vite@latest vue3-vite-mpa -- --template vue
cd vue3-vite-mpa
npm install vue-router@4 pinia

Vite 配置文件

// vite.config.js
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";export default defineConfig({plugins: [vue()],build: {rollupOptions: {input: {home: resolve(__dirname, "src/pages/home/index.html"),about: resolve(__dirname, "src/pages/about/index.html"),admin: resolve(__dirname, "src/pages/admin/index.html"),},},},resolve: {alias: {"@": resolve(__dirname, "src"),},},
});

页面开发示例

首页 (Home)

<!-- src/pages/home/index.html -->
<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>首页 - Vue3 MPA</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body>
</html>
// src/pages/home/main.js
import { createApp } from "vue";
import { createPinia } from "pinia";
import App from "./App.vue";
import router from "@/router";const app = createApp(App);
const pinia = createPinia();app.use(pinia);
app.use(router);
app.mount("#app");
<!-- src/pages/home/App.vue -->
<template><div class="home"><Header /><main class="main-content"><h1>欢迎来到首页</h1><p>这是一个Vue3 + Vite多页面应用</p><div class="navigation"><router-link to="/about">关于我们</router-link><router-link to="/admin">管理后台</router-link></div></main><Footer /></div>
</template><script setup>
import Header from "@/components/Header.vue";
import Footer from "@/components/Footer.vue";
</script><style scoped>
.home {min-height: 100vh;display: flex;flex-direction: column;
}.main-content {flex: 1;padding: 2rem;text-align: center;
}.navigation a {margin: 0 1rem;padding: 0.5rem 1rem;background: #42b883;color: white;text-decoration: none;border-radius: 4px;
}
</style>

共享组件

Header 组件

<!-- src/components/Header.vue -->
<template><header class="header"><nav class="nav"><div class="logo"><a href="/">Vue3 MPA</a></div><ul class="nav-links"><li><a href="/">首页</a></li><li><a href="/about">关于</a></li><li><a href="/admin">管理</a></li></ul></nav></header>
</template><style scoped>
.header {background: #2c3e50;color: white;padding: 1rem 0;
}.nav {max-width: 1200px;margin: 0 auto;display: flex;justify-content: space-between;align-items: center;padding: 0 2rem;
}.logo a {color: white;text-decoration: none;font-size: 1.5rem;font-weight: bold;
}.nav-links {display: flex;list-style: none;gap: 2rem;
}.nav-links a {color: white;text-decoration: none;transition: color 0.3s;
}.nav-links a:hover {color: #42b883;
}
</style>

路由配置

// src/router/index.js
import { createRouter, createWebHistory } from "vue-router";const routes = [{path: "/",name: "Home",component: () => import("@/pages/home/App.vue"),},{path: "/about",name: "About",component: () => import("@/pages/about/App.vue"),},{path: "/admin",name: "Admin",component: () => import("@/pages/admin/App.vue"),},
];const router = createRouter({history: createWebHistory(),routes,
});export default router;

状态管理

// src/stores/user.js
import { defineStore } from "pinia";export const useUserStore = defineStore("user", {state: () => ({user: null,isLoggedIn: false,}),actions: {login(userData) {this.user = userData;this.isLoggedIn = true;},logout() {this.user = null;this.isLoggedIn = false;},},
});

构建和部署

# 开发模式
npm run dev# 生产构建
npm run build# 预览构建结果
npm run preview

性能优化

// vite.config.js 代码分割配置
export default defineConfig({build: {rollupOptions: {input: {home: resolve(__dirname, "src/pages/home/index.html"),about: resolve(__dirname, "src/pages/about/index.html"),admin: resolve(__dirname, "src/pages/admin/index.html"),},output: {manualChunks: {vendor: ["vue", "vue-router"],utils: ["@/utils/common.js"],},},},},
});

总结

Vue3+Vite MPA 多页面应用开发提供了灵活的项目架构,适合大型项目或需要 SEO 的场景。通过合理的项目结构设计、组件复用、路由管理和状态管理,可以构建出高性能、易维护的多页面应用。

关键要点:

  • 每个页面独立,便于开发和维护
  • 共享组件和工具函数,提高代码复用性
  • 使用 Vite 的构建优化,提升开发和生产性能
  • 合理的路由设计,提供良好的用户体验

 Vue3+Vite MPA多页面应用开发完整指南 - 从零搭建到部署优化 - 高质量源码分享平台-免费下载各类网站源码与模板及前沿技术分享

http://www.lryc.cn/news/624880.html

相关文章:

  • 博客项目 Spring + Redis + Mysql
  • Linx--MySQL--安装笔记详细步骤!
  • B4265 [朝阳区小学组 2019] rectangle
  • SpringAI集成MCP
  • CentOS 7更换国内镜像源
  • SQL Server 基本语法
  • 传统方式部署(RuoYi-Cloud)微服务
  • 云原生:重塑软件世界的技术浪潮与编程语言选择
  • 使用websockets中的一些问题和解决方法
  • 华曦达港股IPO观察丨以创新研发为笔,构建AI Home智慧生活新蓝图
  • 8月更新!Windows 10 22H2 64位 五合一版【原版+优化版、版本号:19045.6159】
  • 大模型备案材料—《安全评估报告》撰写指南
  • Zookeeper 在 Kafka 中扮演了什么角色?
  • 8.18作业
  • Python实战--基于Django的企业资源管理系统
  • 嵌入式学习硬件I.MX6ULL(五)按键 中断 GIC OCP原则
  • seuratv4数据结构
  • 软考 系统架构设计师系列知识点之杂项集萃(129)
  • 【数模国奖冲刺】备赛过程中的常见问题
  • Jmeter对图片验证码的处理
  • vue3 + antd实现简单的图片点开可以缩小放大查看
  • 视觉语言导航(4)——强化学习的三种方法 与 优化算法 2.43.4
  • BeeWorks 私有化会议系统:筑牢企业会议安全防线,赋能高效协同
  • Go并发编程-goroutine
  • 私有化部署本地大模型+function Calling+本地数据库
  • 【秋招笔试】2025.08.17字节跳动秋招机考真题
  • 技术赋能安全:智慧工地构建城市建设新防线
  • IB数学课程知识点有哪些?IB数学课程辅导机构怎么选?
  • [系统架构设计师]未来信息综合技术(十一)
  • 【秋招笔试】2025.08.17大疆秋招机考第一套