```js
const routes = [
- { path: '/custom-transition', meta: { transition: 'slide-left' } },
- { path: '/other-transition', meta: { transition: 'slide-right' } },
+ {
+ path: '/custom-transition',
+ component: PanelLeft,
+ meta: { transition: 'slide-left' },
+ },
+ {
+ path: '/other-transition',
+ component: PanelRight,
+ meta: { transition: 'slide-right' },
+ },
]
```
```js
const routes = [
// will match everything and put it under `$route.params.pathMatch`
- { path: '/:pathMatch(.*)*', name: 'NotFound' },
+ { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },
// will match anything starting with `/user-` and put it under `$route.params.afterUser`
- { path: '/user-:afterUser(.*)' },
+ { path: '/user-:afterUser(.*)', component: UserGeneric },
]
```
Note that **[Navigation Guards](../advanced/navigation-guards.md) are not applied on the route that redirects, only on its target**. e.g. In the example below, adding a `beforeEnter` guard to the `/home` route would not have any effect.
+When writing a `redirect`, you can omit the `component` option because it is never directly reached so there is no component to render. The only exception are [nested routes](./nested-routes.md): if a route record has `children` and a `redirect` property, it should also have a `component` property.
+
### Relative redirecting
It's also possible to redirect to a relative location:
const routes = [
{
path: '/users',
+ component: UsersLayout,
children: [
// this will render the UserList for these 3 URLs
// - /users
const routes = [
{
path: '/users/:id',
+ component: UsersByIdLayout,
children: [
// this will render the UserDetails for these 3 URLs
// - /users/24
Most applications will use static routes like `/about` and dynamic routes like `/users/:userId` like we just saw in [Dynamic Route Matching](./dynamic-matching.md), but Vue Router has much more to offer!
+:::tip
+For the sake of simplicity, all, route records **are omitting the `component` property** to focus on the `path` value.
+:::
+
## Custom Regexp in params
When defining a param like `:userId`, we internally use the following regexp `([^/]+)` (at least one character that isn't a slash `/`) to extract params from URLs. This works well unless you need to differentiate two routes based on the param content. Imagine two routes `/:orderId` and `/:productName`, both would match the exact same URLs, so we need a way to differentiate them. The easiest way would be to add a static section to the path that differentiates them:
```js
// given the following route:
-const routes = [{ path: '/users/:id', name: 'users' }]
+const routes = [{ path: '/users/:id', name: 'user', component: UserDetails }]
// Missing the `id` param will fail
-router.push({ name: 'users' })
-router.resolve({ name: 'users' })
+router.push({ name: 'user' })
+router.resolve({ name: 'user' })
```
**Reason**: Same as above.
{
path: '/dashboard',
name: 'dashboard-parent',
+ component: DashboardParent
children: [
- { path: '', name: 'dashboard' },
- { path: 'settings', name: 'dashboard-settings' },
+ { path: '', name: 'dashboard', component: DashboardDefault },
+ { path: 'settings', name: 'dashboard-settings', component: DashboardSettings },
],
},
]
const routes = [
{
path: '/parent',
+ component: Parent,
children: [
// this would now redirect to `/home` instead of `/parent/home`
{ path: '', redirect: 'home' },
- { path: 'home' },
+ { path: 'home', component: Home },
],
},
]
const routes = [
// pathMatch is the name of the param, e.g., going to /not/found yields
// { params: { pathMatch: ['not', 'found'] }}
- { path: '/:pathMatch(.*)*', name: 'not-found' },
+ { path: '/:pathMatch(.*)*', name: 'not-found', component: NotFound },
// if you omit the last `*`, the `/` character in params will be encoded when resolving or pushing
- { path: '/:pathMatch(.*)', name: 'bad-not-found' },
+ { path: '/:pathMatch(.*)', name: 'bad-not-found', component: NotFound },
]
// bad example:
router.resolve({