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

48 lines
835 B
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Vue Routes 4. x
Vue Router 官网: https://router.vuejs.org/zh/
变化: https://router.vuejs.org/zh/guide/migration/
## new Router 变成 createRouter
```js
// 以前是
// 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 参数
```js
const routes = [
{ path: '/:pathMatch(.*)*', name: 'not-found', component: NotFound }
]
```
主动跳转 404
```js
router.push('/not/found')
// 或者
router.push({
name: 'not-found',
params: { pathMatch: ['not', 'found'] }
})
```