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

解决 Vue SPA 刷新导致 404 的问题

解决 Vue SPA 刷新导致 404 的问题

  • 解决 Vue SPA 刷新导致 404 的问题
    • 问题描述
    • 解决方案
      • 1. 后端配置:捕获所有路由返回 index.html
        • FastAPI 配置示例
      • 2. 使用专门的 SPA 中间件
      • 3. 使用 Nginx 作为反向代理
      • 4. Vue Router 配置
      • 5. 构建配置
    • 完整示例项目结构
    • 部署注意事项

解决 Vue SPA 刷新导致 404 的问题

问题描述

通过vue展示页面,在地址栏中会出现前端的地址导航栏,而用户点击浏览器中的刷新按钮时,浏览器会将地址栏中前端的导航路径发送get请求到后台,导致404

解决方案

这是一个非常常见的问题,当使用 Vue.js 等前端框架构建单页面应用(SPA)并在浏览器中刷新时,由于前端路由路径被直接发送到后端服务器,而服务器没有对应的路由处理,导致返回 404 错误。

1. 后端配置:捕获所有路由返回 index.html

最常用的解决方案是在后端服务器配置中,将所有非 API 和非静态资源的请求都重定向到 Vue 应用的入口文件(通常是 index.html)。

FastAPI 配置示例
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
import osapp = FastAPI()# 服务静态文件(CSS、JS、图片等)
app.mount("/static", StaticFiles(directory="dist/static"), name="static")# API 路由
@app.get("/api/data")
async def get_data():return {"message": "API response"}# 捕获所有前端路由,返回 index.html
@app.get("/{full_path:path}")
async def serve_spa(request: Request, full_path: str):# 检查请求的路径是否是 API 或静态文件if full_path.startswith("api/") or full_path.startswith("static/"):# 如果是 API 或静态文件路径,返回 404return {"error": "Not found"}# 否则返回 Vue 应用的 index.htmlreturn FileResponse("dist/index.html")if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=8000)

2. 使用专门的 SPA 中间件

对于更复杂的场景,可以使用专门的中间件来处理 SPA 路由:

from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
import osapp = FastAPI()# 服务静态文件
app.mount("/static", StaticFiles(directory="dist/static"), name="static")# API 路由
@app.get("/api/data")
async def get_data():return {"message": "API response"}# SPA 中间件
@app.middleware("http")
async def spa_middleware(request: Request, call_next):response = await call_next(request)# 如果响应是 404 并且请求不是 API 或静态文件if response.status_code == 404 and not (request.url.path.startswith("/api/") or request.url.path.startswith("/static/")):return FileResponse("dist/index.html")return response

3. 使用 Nginx 作为反向代理

如果使用 Nginx 作为反向代理,可以在 Nginx 配置中处理路由:

server {listen 80;server_name your-domain.com;# 静态文件服务location /static/ {alias /path/to/your/app/dist/static/;expires 1y;add_header Cache-Control "public, immutable";}# API 路由location /api/ {proxy_pass http://localhost:8000;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;}# 所有其他路由返回 index.htmllocation / {try_files $uri $uri/ /index.html;root /path/to/your/app/dist;}
}

4. Vue Router 配置

确保 Vue Router 使用 history 模式:

import { createRouter, createWebHistory } from 'vue-router'const router = createRouter({history: createWebHistory(),routes: [// 路由配置]
})

5. 构建配置

在 Vue 项目的 vue.config.js 中设置 publicPath:

module.exports = {publicPath: '/',// 其他配置...
}

完整示例项目结构

project/
├── backend/
│   ├── main.py          # FastAPI 后端
│   └── requirements.txt
├── frontend/
│   ├── src/
│   ├── public/
│   ├── dist/            # 构建后的文件
│   └── vue.config.js
└── README.md

部署注意事项

  1. 构建前端应用:在部署前,确保已经构建了 Vue 应用(npm run build
  2. 路径匹配:确保后端正确处理所有前端路由路径
  3. 静态文件服务:正确配置静态文件服务路径
  4. API 路由前缀:建议为所有 API 路由添加统一前缀(如 /api/

通过以上配置,当用户在浏览器中刷新任何前端路由时,服务器都会返回 Vue 应用的 index.html 文件,然后由 Vue Router 在前端处理路由,从而避免 404 错误。


愿你我都能在各自的领域里不断成长,勇敢追求梦想,同时也保持对世界的好奇与善意!

http://www.dtcms.com/a/389221.html

相关文章:

  • 大型语言模型 (LLMs) 的演进历程:从架构革命到智能涌现
  • 大语言模型为什么要叫【模型】
  • 教程上新丨ACL机器翻译大赛30个语种摘冠,腾讯Hunyuan-MT-7B支持33种语言翻译
  • 《C++程序设计》笔记
  • NVR接入录像回放平台EasyCVR海康设备视频平台视频监控系统常见故障与排查全解析
  • 半导体制造常提到的Fan-Out晶圆级封装是什么?
  • Qt 系统相关 - 文件
  • P2242 公路维修问题
  • 安装wsl
  • 牛客多校04C :Computational Geometry Problem(p-Dyck路计数)
  • CMake+visual studio 2022 +qt6 , 从Linux平台移植到windows下平台开发
  • 大模型系列——Playwright MCP 可以复用 Chrome 登录态了
  • 三星S25 Edge 与iPhone 17 Air:最新对比
  • 电脑怎么连接wifi?【图文详解】笔记本电脑怎么连接无线wifi?笔记本电脑连不上wifi怎么办?
  • 设计模式-代理模式详解
  • 怎样让AI图生3D更加高质高效
  • Java 集合框架 Set 接口:实现类的底层数据结构与核心特点
  • 【大模型】使用Qwen-VL大模型进行验证码识别的完整指南
  • 深度学习体系化入门:从理论到实践的完整框架
  • 餐饮行业系统集成分享:OMS 订单数据推送ERP 核算
  • 深入剖析OpenHarmony ClearPlay DRM驱动:从HDI接口到动态加载的完整实现路径
  • [WesternCTF2018]shrine
  • 硬件 - RK3588部分(2) - 原理图 - 最小系统
  • Android进阶之路 - 从 URL Scheme 到 Deep Link 与 App Link
  • MySQL监控Shell脚本实战指南
  • 【Android】Jetpack Media3 如何播放音频文件 实现视频播放器
  • Android 开发 集成 uni 小程序,并实现相互通信
  • 【office】怎么设置第一章二级标题为1.1 1.2 1.3然后第二章为2.1 2.2 2.3这样子
  • JVM的垃圾回收机制(一次完整的GC流程)
  • 拥抱新一代 Web 3D 引擎,Three.js 项目快速升级 Galacean 指南