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: '/'
    }
  ]
})
Logo

前往低代码交流专区

更多推荐