Using @angular/router version 3.0.0-beta.2 , I would like to create an application that allows my users to view the file system.
The following are the types of URLs that I would like to support:
http://myapp/browse <--- Browse without parameters, lists root dir http://myapp/browse/animals <--- Browse "animals" subdirectory http://myapp/browse/animals/mammals <--- Browse "animals/mammals" subdirectory
I am trying to create a mechanism for configuration using RouterConfig .
Path Style Options
{ path: 'browse/:path', component: BrowserComponent }
This RouterConfig supports only one subdirectory, i.e. browse/animals . However, if I try to go to the second level subdirectory /browse/animals/mammals , I get the following error:
Error: Cannot match any routes: '/browse/animals/mammals'
I understand that the default behavior of the parameter in the router (in this case :path ) means to “split” the first forward slash and assume that the next token is a subpath.
How to configure the router so that :path accepts a forward slash / gobbles up the remaining URL as a parameter?
Optional parameter
How can I deal with a use case when a user wants to view the root directory, i.e. URL /browse . This will not match the router configuration described above, because the :path parameter :path missing.
I tried to get around this using two well-tuned router paths:
{ path: 'browse', component: BrowserComponent }, { path: 'browse/:path', component: BrowserComponent }
However, this causes me problems with routerLinkActive . I have a main menu with a link to a browser that looks like this:
<a [routerLink]="['/browse']" [routerLinkActive]="['active']">...</a>
I would like this link in the main menu to have an active class if the user looked at the root directory /browse or in some subdirectory /browse/animals/ .
However, if the URL /browse/animals/ not a child of the /browse route, it does not become active .
I cannot make the :path parameter route the child of the /browse root, as there is no need for a nested view, and this results in:
Cannot find primary outlet to load BrowserComponent