|
1 |
| -# Route Context |
| 1 | +# Route Object |
| 2 | + |
| 3 | +Vue-router supports matching paths that contain dynamic segments, star segments and query strings. All these information of a parsed route will be accessible on the exposed **Route Context Objects** (we will just call them "route" objects from now on). The route object will be injected into every component in a vue-router-enabled app as `this.$route`, and will be updated whenever a route transition is performed. |
| 4 | + |
| 5 | +A route object exposes the following properties: |
| 6 | + |
| 7 | +- **$route.path** |
| 8 | + |
| 9 | + A string that equals the path of the current route, always resolved as an absolute path. e.g. `"/foo/bar"`. |
| 10 | + |
| 11 | +- **$route.params** |
| 12 | + |
| 13 | + An object that contains key/value pairs of dynamic segments and star segments. More details below. |
| 14 | + |
| 15 | +- **$route.query** |
| 16 | + |
| 17 | + An object that contains key/value pairs of the query string. For example, for a path `/foo?user=1`, we get `$route.query.user == 1`. |
| 18 | + |
| 19 | +### Using in Templates |
| 20 | + |
| 21 | +You can directly bind to the `$route` object inside your component templates. For example: |
| 22 | + |
| 23 | +``` html |
| 24 | +<div> |
| 25 | + <p>Current route path: {{$route.path}}</p> |
| 26 | + <p>Current route params: {{$route.params | json}}</p> |
| 27 | +</div> |
| 28 | +``` |
| 29 | + |
| 30 | +### Dynamic Segments |
| 31 | + |
| 32 | +Dynamic segments can be defined in the form of path segments with a leading colon, e.g. in `user/:username`, `:username` is the dynamic segment. It will match paths like `/user/foo` or `/user/bar`. When a path containing a dynamic segment is matched, the dynamic segments will be available inside `$route.params`. |
| 33 | + |
| 34 | +Example Usage: |
| 35 | + |
| 36 | +``` js |
| 37 | +router.map({ |
| 38 | + '/user/:username': { |
| 39 | + component: { |
| 40 | + template: '<p>username is {{$route.params.username}}</p>' |
| 41 | + } |
| 42 | + } |
| 43 | +}) |
| 44 | +``` |
| 45 | + |
| 46 | +A path can contain multiple dynamic segments, and each of them will be stored as a key/value pair in `$route.params`. |
| 47 | + |
| 48 | +Examples: |
| 49 | + |
| 50 | +| pattern | matched path | $route.params | |
| 51 | +|---------|------|--------| |
| 52 | +| /user/:username | /user/evan | `{ username: 'evan' }` | |
| 53 | +| /user/:username/post/:post_id | /user/evan/post/123 | `{ username: 'evan', post_id: 123 }` | |
| 54 | + |
| 55 | +### Star Segments |
| 56 | + |
| 57 | +While dynamic segments can correspond to only a single segment in a path, star segments is basically the "greedy" version of it. For example `/foo/*bar` will match anything that starts with `/foo/`. The part matched by the star segment will also be available in `$route.params`. |
| 58 | + |
| 59 | +Examples: |
| 60 | + |
| 61 | +| pattern | matched path | $route.params | |
| 62 | +|---------|------|--------| |
| 63 | +| /user/*any | /user/a/b/c | `{ any: 'a/b/c' }` | |
| 64 | +| /foo/*any/bar | /foo/a/b/bar | `{ any: 'a/b' }` | |
0 commit comments