簡體   English   中英

如何在 Vue.js 3 中使用 vue 路由器將 404 路由重定向到 Home

[英]How to redirect a 404 route to Home using vue router in Vue.js 3

全能社區您好!

我對 Vue.js 3 有一點問題。在路由器中,我無法定義任何未知路由都應該重定向到“/”。

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
 {
    path: "/*",
    redirect: "/",
  }
]

在這里,我測試了重定向:“/”有效,但路徑:“/*”無效。 路徑“/ ”僅適用於 domain.com/ ......我也測試了路徑:“*”但我收到這個錯誤:

“未捕獲的錯誤:路由” “應該是“/ ”。

你如何在最新的 Vue.js3 中進行 404 重定向?

在與 Vue 3 兼容的 Vue 路由器 4 中,您可以Catch all / 404 Not found Route如下:

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: "/",
  }
]

現場演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM