|
| 1 | +# The Route Object |
| 2 | + |
| 3 | +A **route object** represents the state of the current active route. It contains parsed information of the current URL and the **route records** matched by the URL. |
| 4 | + |
| 5 | +The route object is immutable. Every successful navigation will result in a fresh route object. |
| 6 | + |
| 7 | +The route object can be found in multiple places: |
| 8 | + |
| 9 | +- Inside components as `this.$route`, and obvious inside `$route` watcher callbacks; |
| 10 | + |
| 11 | +- As the return value of calling `router.match(location)`; |
| 12 | + |
| 13 | +- Inside navigation guards as the first argument: |
| 14 | + |
| 15 | + ``` js |
| 16 | + router.beforeEach((route, redirect, next) => { |
| 17 | + // route here is a route object |
| 18 | + }) |
| 19 | + ``` |
| 20 | + |
| 21 | +- Inside the `scrollBehavior` function as arguments: |
| 22 | + |
| 23 | + ``` js |
| 24 | + const router = new VueRouter({ |
| 25 | + scrollBehavior (to, from, savedPosition) { |
| 26 | + // to and from are both route objects |
| 27 | + } |
| 28 | + }) |
| 29 | + ``` |
| 30 | + |
| 31 | +### Route Object Properties |
| 32 | + |
| 33 | +- **$route.path** |
| 34 | + |
| 35 | + - type: `string` |
| 36 | + |
| 37 | + A string that equals the path of the current route, always resolved as an absolute path. e.g. `"/foo/bar"`. |
| 38 | + |
| 39 | +- **$route.params** |
| 40 | + |
| 41 | + - type: `Object` |
| 42 | + |
| 43 | + An object that contains key/value pairs of dynamic segments and star segments. If there are no params the value will be an empty object. |
| 44 | + |
| 45 | +- **$route.query** |
| 46 | + |
| 47 | + - type: `Object` |
| 48 | + |
| 49 | + 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`. If there is no query the value will be an empty object. |
| 50 | + |
| 51 | +- **$route.hash** |
| 52 | + |
| 53 | + - type: `string` |
| 54 | + |
| 55 | + The hash of the current route (without `#`), if it has one. If no hash is present the value will be an empty string. |
| 56 | + |
| 57 | +- **$route.fullPath** |
| 58 | + |
| 59 | + - type: `string` |
| 60 | + |
| 61 | + The full resolved URL including query and hash. |
| 62 | + |
| 63 | +- **$route.matched** |
| 64 | + |
| 65 | + - type: `Array<RouteRecord>` |
| 66 | + |
| 67 | + An Array containing **route records** for all nested path segments of the current route. Route records are the copies of the objects in the `routes` configuration Array (and in `children` Arrays): |
| 68 | + |
| 69 | + ``` js |
| 70 | + const router = new VueRouter({ |
| 71 | + routes: [ |
| 72 | + // the following object is a route record |
| 73 | + { path: '/foo', component: Foo, |
| 74 | + children: [ |
| 75 | + // this is also a route record |
| 76 | + { path: 'bar', component: Bar } |
| 77 | + ] |
| 78 | + } |
| 79 | + ] |
| 80 | + }) |
| 81 | + ``` |
| 82 | + |
| 83 | + When the URL is `/foo/bar`, `$route.matched` will be an Array containing both objects (cloned), in parent to child order. |
| 84 | + |
| 85 | +- **$route.name** |
| 86 | + |
| 87 | + The name of the current route, if it has one. (See [Named Routes](../essentials/named-routes.md)) |
0 commit comments