Vue访问不存在的路由,重定向跳转

Vue访问不存在的路由,重定向跳转

src/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

// 重加载push方法,解决报错
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: () => import('@/components/Home')
    },
    {
      path: '/demo',
      name: 'Demo',
      component: () => import('@/components/Demo')
    },
    {
      path: '/404',
      name: '404',
      component: () => import('@/components/404')
    },
    {
      path: '*', // 重定向页面地址
      redirect: '/'
    }
  ]
})
posted @ 2022-12-06 22:18  轻风细雨_林木木  阅读(40)  评论(0编辑  收藏  举报