Using the @angular/router
version 3.0.0-beta.2
I would like to build an application which allows my users to browse a filesystem.
The following illustrates the types of URL 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 struggling to come up with a mechanism to configure this using RouterConfig
.
Path-style Parameters
{ path: 'browse/:path', component: BrowserComponent }
This RouterConfig
only supports a single subdirectory, i.e. browse/animals
. However, if I attempt to browse to the second-level of subdirectory /browse/animals/mammals
I get the following error:
Error: Cannot match any routes: '/browse/animals/mammals'
I understand that the default behaviour of a parameter in the router (in this case :path
) is to "break" on the first forward slash, and assume the following token is a sub-path.
How can I configure the router to allow the :path
parameter to accept forward slashes / "gobble" the entire remaining URL as the parameter?
Optional Parameter
How can I deal with the use-case where the user wishes to browse the root directory, i.e. the URL /browse
. This will not match the router configuration described above, as the :path
parameter is not present.
I have attempted to work around this using two distinctly configured router paths:
{ path: 'browse', component: BrowserComponent },
{ path: 'browse/:path', component: BrowserComponent }
However, this is causing me problems with routerLinkActive
. I have a main menu with a link to the browser, that looks like this:
<a [routerLink]="['/browse']" [routerLinkActive]="['active']">...</a>
I would like this main menu link to have the active
class if the user has browsed to the root /browse
or to some subdirectory /browse/animals/
.
However, given the /browse/animals/
URL is not a child of the /browse
route, it does not become active
.
I cannot make the :path
parameter route a child of the /browse
root, as there is no need for a nested view, and it results in:
Cannot find primary outlet to load BrowserComponent