question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional parameters

See original GitHub issue

Hi,

I currently have a route structure that looks like this:

  • Main (all routes) – Home (/:continent?) – Entity (/:country/:region/:city)

The continent path is optional. How can I achieve this with Router5? The only requirement that I have is that both / and /africa (for instance) have the same parent (Home) route. I’ve tried fiddling with absolute paths, and regexes with empty strings, but all approaches break down at some point.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
trochcommented, Oct 2, 2017

Optional parameters are not supported in router5 (a design decision), and the way to achieve it is to define two routes (one with param, one without) with a parent forwarding to the non-param one by default (using forwardTo route option).

Don’t forget to set router5 option useTrailingSlash: false for it to strip trailing slashes when building (or set it to true if you want trailing slashes to always be added).

[
	{
		name: 'section',
		path: '/',
		forwardTo: 'section.home'
	},
	{
		name: 'section.home',
		path: '/'
	},
	{
		name: 'section.subsection',
		path: '/:subsectionParam'
	}
]

In your case, something like:

[
	{
		name: 'continent',
		path: '/',
		forwardTo: 'continent.default'
	},
	{
		name: 'continent.default',
		path: '/'
	},
	{
		name: 'continent.view',
		path: '/:continent'
	}
]
1reaction
klimashkincommented, Feb 13, 2018

Now I see, didn’t notice that in changelog, thanks! But seems it doesn’t work in that case

I have

{name: "app", path: "?:showVersion"},
{name: "app.ip", path: "/ip"},
{name: "app.ip.list", path: "/?:sort?:page"},
{name: "app.ip.item", path: "/:id", forwardTo: "app.ip.item.view"},
{name: "app.ip.item.edit", path: "/edit"},
{name: "app.ip.item.view", path: "/:version", defaultParams: {version: 'active'}},

And if I navigate to /ip/5 I get Uncaught Error: Cannot build path: '/:version' requires missing parameters { version } at Path.build$$1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Named and Optional Arguments - C# Programming Guide
Named arguments enable you to specify an argument for a parameter by matching the argument with its name rather than with its position...
Read more >
C# | Optional Parameters - GeeksforGeeks
The optional parameters are always defined at the end of the parameter list. Or in other words, the last parameter of the method,...
Read more >
What is an Optional parameter in C#? - Tutorialspoint
It means we call method without passing the arguments. The optional parameter contains a default value in function definition. If we do not...
Read more >
Optional Parameters in Java: Common Strategies and ...
Learn how to structure your code and build objects with a large number of optional parameters, in a readable and well-structured manner.
Read more >
Declaring optional function parameters in JavaScript - Flexiple
By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found