登录  /  注册
Vue 3 + Vite 开发服务器中的动态路由导致页面重新加载时出现 404 错误
P粉811329034
P粉811329034 2023-10-27 08:49:33
[Vue.js讨论组]

在我的项目中我使用 Vue 3.2.36 + Vite 2.9.9 + VueRouter 4.1.3

我使用 npm run dev 运行开发服务器。

我的路线定义:

routes: [
    {
      path: '/',
      component: Home,
    },
    {
      path: '/about',
      component: () => import('@/views/About.vue'),
    },
    {
      path: '/user-details/:login',
      name: 'userDetails',
      component: () => import('@/views/User.vue'),
    },
    {
      path: '/:pathMatch(.*)*',
      component: NotFound,
    }
  ],

当我使用 router.push({name: 'userDetails', params: {login: 'john.smith'}}) 以编程方式导航时,userDetails 页面/组件正确显示。

但是如果我浏览器重新加载开发服务器返回 404 并且 NotFound 组件不会显示。

Chrome:


FF:


工作示例:此处

我已将问题隔离给Vite。使用 Vue CLI 一切正常。

我的vite.config.js

import loadVersion from 'vite-plugin-package-version';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

export default defineConfig(() => {
  return {
    server: {
      port: 8080,
    },
    plugins: [vue(), loadVersion()],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    envDir: './src/configuration',
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/scss/_variables.scss";',
        },
      },
    },
  };
});

检查了我的index.html


检查了 vue-router 历史模式文档,并在警告部分中指出,如果未找到路由,路由器应默认为 index.html ,并且它使用 Vue CLI 执行此操作,但不使用 Vite 执行此操作。

P粉811329034
P粉811329034

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学