完全消除Vue3项目短暂跳转到404的现象

有个Vue项目,上传服务器后出现了首屏加载后短暂跳转到404的情况,因为并非在根目录下放置index.html和assets文件夹,而是放在了/a/目录下。
解决方法分为3步。
第一步:修改vite.config.js

base: '/a/', //新增这一行吗,即基础目录
server: {
port: 3000
}

第二步:修改 router/index.js
``js
import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes/index.js'

const router = createRouter({
// 🆕 必须设置与 Vite 相同的基础路径
history: createWebHistory('/a/'),
routes
})

export default router

第三步: 完全消除 NotFound 闪烁,修改router/routes/index.js
``js
const routes = [
// 🆕 添加对 index.html 的重定向
{
path: '/index.html',
redirect: '/'
},

// 现有路由配置...
...entryRoutes,
...examRoutes,
// ...
]

posted @ 2025-12-01 09:43  充实地生活着  阅读(0)  评论(0)    收藏  举报