Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(dynamic-matching): add optional param on docs #3407

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/guide/essentials/dynamic-matching.md
Expand Up @@ -35,9 +35,26 @@ You can have multiple dynamic segments in the same route, and they will map to c
| ----------------------------- | ------------------- | -------------------------------------- |
| /user/:username | /user/evan | `{ username: 'evan' }` |
| /user/:username/post/:post_id | /user/evan/post/123 | `{ username: 'evan', post_id: '123' }` |
| /user/:username/post/:post_id?| /user/evan/post | `{ username: 'evan' }` |

In addition to `$route.params`, the `$route` object also exposes other useful information such as `$route.query` (if there is a query in the URL), `$route.hash`, etc. You can check out the full details in the [API Reference](../../api/#the-route-object).

## Optional Param

often we will need to map the same route with and without param to the same component. For example, we may have a `User` component which should be rendered for all users or a specific ID. In `vue-router` we can use a dynamic segment in the path to achieve that:
```js
const User = {
template: '<div>User</div>'
}

const router = new VueRouter({
routes: [
// dynamic segments start with a colon
{ path: '/user/:id?', component: User }
]
})
```

## Reacting to Params Changes

One thing to note when using routes with params is that when the user navigates from `/user/foo` to `/user/bar`, **the same component instance will be reused**. Since both routes render the same component, this is more efficient than destroying the old instance and then creating a new one. **However, this also means that the lifecycle hooks of the component will not be called**.
Expand Down