devStandard/docs/learning/5-vue/2-Vue Routes 4.x.md
2025-03-29 14:35:49 +08:00

835 B

Vue Routes 4. x

Vue Router 官网: https://router.vuejs.org/zh/

变化: https://router.vuejs.org/zh/guide/migration/

new Router 变成 createRouter

// 以前是
// import Router from 'vue-router'
import { createRouter } from 'vue-router'

const router = createRouter({
  // ...
})

新的 history 配置取代 mode

mode: 'history' 配置已经被一个更灵活的 history 配置所取代

  • "history"createWebHistory()
  • "hash"createWebHashHistory()
  • "abstract"createMemoryHistory()

删除了通配符路由

使用 regex 参数

const routes = [
  { path: '/:pathMatch(.*)*', name: 'not-found', component: NotFound }
]

主动跳转 404

router.push('/not/found')

// 或者
router.push({
  name: 'not-found',
  params: { pathMatch: ['not', 'found'] }
})