|
| 1 | +# Route Matching Syntax |
| 2 | + |
| 3 | +Most applications will use static routes like `/about` and dynamic routes like `/users/:userId`, but Vue Router has much more to offer! |
| 4 | + |
| 5 | +## Custom Regexp in params |
| 6 | + |
| 7 | +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: |
| 8 | + |
| 9 | +```js |
| 10 | +const routes = [ |
| 11 | + // matches /o/3549 |
| 12 | + { path: '/o/:orderId' }, |
| 13 | + // matches /p/books |
| 14 | + { path: '/p/:productName' }, |
| 15 | +] |
| 16 | +``` |
| 17 | + |
| 18 | +But in some scenarios we don't want to add that static section `/o`/`p`. However, `orderId` is always a number while `productName` can be anything, so we can specify a custom regexp for a param in parentheses: |
| 19 | + |
| 20 | +```js |
| 21 | +const routes = [ |
| 22 | + // /:orderId -> matches only numbers |
| 23 | + { path: '/:orderId(\\d+)' }, |
| 24 | + // /:productName -> matches anything else |
| 25 | + { path: '/:productName' }, |
| 26 | +] |
| 27 | +``` |
| 28 | + |
| 29 | +Make sure to **escape backslashes `\`** like we did with `\d` to actually pass the backslash character to a string in JavaScript. |
| 30 | + |
| 31 | +## Repeatable params |
| 32 | + |
| 33 | +If you need to match routes with multiple sections like `/first/second/third`, you can mark a param as repeatable with `*` (0 or more) and `+` (1 or more): |
| 34 | + |
| 35 | +```js |
| 36 | +const routes = [ |
| 37 | + // /:chapters -> matches /one, /one/two, /one/two/three, etc |
| 38 | + { path: '/:chapters+' }, |
| 39 | + // /:chapters -> matches /, /one, /one/two, /one/two/three, etc |
| 40 | + { path: '/:chapters*' }, |
| 41 | +] |
| 42 | +``` |
| 43 | + |
| 44 | +This will give you an array of params instead of a string and will also require you to pass an array when using named routes: |
| 45 | + |
| 46 | +```js |
| 47 | +// given { path: '/:chapters*', name: 'chapters' }, |
| 48 | +router.resolve({ name: 'chapters', params: { chapters: [] } }).href |
| 49 | +// produces / |
| 50 | +router.resolve({ name: 'chapters', params: { chapters: ['a', 'b'] } }).href |
| 51 | +// produces /a/b |
| 52 | + |
| 53 | +// given { path: '/:chapters+', name: 'chapters' }, |
| 54 | +router.resolve({ name: 'chapters', params: { chapters: [] } }).href |
| 55 | +// throws an Error because `chapters` is empty |
| 56 | +``` |
| 57 | + |
| 58 | +These can also be combined with custom Regexp by adding them **after the closing parentheses**: |
| 59 | + |
| 60 | +```js |
| 61 | +const routes = [ |
| 62 | + // only match numbers |
| 63 | + { path: '/:chapters(\\d+)+' }, |
| 64 | + { path: '/:chapters(\\d+)*' }, |
| 65 | +] |
| 66 | +``` |
| 67 | + |
| 68 | +## Optional parameters |
| 69 | + |
| 70 | +You can also mark a parameter as optional by using the `?` modifier: |
| 71 | + |
| 72 | +```js |
| 73 | +const routes = [ |
| 74 | + // will match /users and /users/posva |
| 75 | + { path: '/users/:userId?' }, |
| 76 | + // will match /users and /users/42 |
| 77 | + { path: '/users/:userId(\\d+)?' }, |
| 78 | +] |
| 79 | +``` |
| 80 | + |
| 81 | +Note that `*` also marks the parameter as optional but cannot be repeated |
| 82 | + |
| 83 | +## Debugging |
| 84 | + |
| 85 | +If you need to dig how your routes are transformed into Regexp to understand why a route isn't being matched or, to report a bug, you can use the [path ranker tool](https://paths.esm.dev). It supports sharing your routes through the URL. |
0 commit comments