在我的專案中我使用 Vue 3.2.36 Vite 2.9.9 VueRouter 4.1.3
我使用 npm run dev
運(yùn)行開(kāi)發(fā)伺服器。
我的路線定義:
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, } ],
當(dāng)我使用router.push({name: 'userDetails', params: {login: 'john.smith'}})
以程式設(shè)計(jì)方式導(dǎo)覽時(shí),userDetails
頁(yè)面/組件正確顯示。
但是如果我瀏覽器重新加載開(kāi)發(fā)伺服器返回 404 並且 NotFound
元件不會(huì)顯示。
Chrome:
FF:
工作範(fàn)例:此處
我已將問(wèn)題隔離給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
歷史模式文檔,並在警告部分中指出,如果未找到路由,路由器應(yīng)預(yù)設(shè)為index.html
,並且它使用Vue CLI 執(zhí)行此操作,但不使用Vite 執(zhí)行此操作。
我的 Web 應(yīng)用程式位於 Azure DevOps 上,但位於 Linux 上 對(duì)於 Linux Web 應(yīng)用程序,您必須新增下一個(gè)命令才能在啟動(dòng)時(shí)執(zhí)行 Web 應(yīng)用程式命令。這對(duì)我有用。
pm2 serve /home/site/wwwroot --no-daemon --spa
您可以在本文中查看更多內(nèi)容 Azure webapp linux 上的 React 應(yīng)用程式