vue3+vue-router4 路由HTML5模式(createWebHistory ),编译后部署到nginx,点击路由跳转正常,但是刷新页面报404

javascript 复制代码
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home',
    },
    {
      path: '/index/home',
      component: () => import('../views/index/Home.vue'),
    },
    {
      path: '/home',
      component: () => import('../views/home/Index.vue'),
    },
    {
      path: '/me',
      component: () => import('../views/me/Index.vue'),
    },
  ],
})

router.beforeEach((to, from) => {
  return true
})

export default router

不兼容:

bash 复制代码
    server {
        listen 80;
        server_name localhost chat.test.com;

        #配置根目录
        location / {
            root    /temp/test;
            index  index.html index.htm;
            add_header Content-Security-Policy upgrade-insecure-requests;
        }
    }  

解决方案:

bash 复制代码
    server {
        listen 80;
        server_name localhost chat.test.com;

        #配置根目录
        location / {
            root    /temp/test;
            #index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            add_header Content-Security-Policy upgrade-insecure-requests;
        }
    }  

参考链接

https://router.vuejs.org/zh/guide/essentials/history-mode.html#nginx

人工智能学习网站

https://chat.xutongbao.top

相关推荐
....4921 小时前
Vue3 + Element Plus 实现可搜索、可折叠、可拖拽的部门树组件
前端·javascript·vue.js
bitbitDown2 小时前
重构缓存时踩的坑:注释了三行没用的代码却导致白屏
前端·javascript·vue.js
Solon阿杰2 小时前
前端(react/vue)实现全景图片(360°)查看器
javascript·vue.js
前端程序猿i3 小时前
用本地代理 + ZIP 打包 + Excel 命名,优雅批量下载跨域 PDF
前端·javascript·vue.js·html
Danny_FD3 小时前
Vue2 中使用vue-markdown实现编辑器
前端·javascript·vue.js
前端赵哈哈3 小时前
Vue 3 + TypeScript 项目模板
前端·vue.js·vite
一只大黑洋4 小时前
Clipboard.js 复制内容
前端·javascript·vue.js
鹏多多4 小时前
vue混入mixins详解和生命周期影响
前端·javascript·vue.js
柯南95274 小时前
Vue 3 响应式系统核心:dep.ts 解析
vue.js
岁月宁静4 小时前
MCP 协议应用场景 —— Cursor 连接 Master Go AI
前端·vue.js·人工智能