I want to configure route for my angular2 application. My URL needs to be like this:
http://domain_name/constant_value/variable_value/constant_value
The url can be like following examples:
http://localhost/myhouse/floor1/mirror
http://localhost/myhouse/floor1/room1/mirror
http://localhost/myhouse/floor1/room1/bathroom/mirror
Here the routes /myhouse and /mirror are constant. But the middle part can be anything like /floor1 or /floor2/something/something/....
How can i define a route for that in routing module.
const routes: Routes = [
{
path: 'myhouse',
children: [
{
path: ..., //here how do i configure it
children: [
{
path: '/mirror',
component: MirrorComponent
}
]
}
]
}
];
Here the mirror component must be loaded if the url has /mirror at the end of the url if not login component should be loaded. Mirror will be loaded for the urls shown above. Each mirror component will have different propertise value inside according to the variable part of the url.
For login component url will be like:
or
or
I tried wanted to use regex but it seems the regex is not supported for newer version of angular2. If I am wrong on not being able to use regex please kindly point me to that direction with an example. If not please point me to right direction.
Try to do that:
Here you will find a better explanation: http://vsavkin.tumblr.com/post/146722301646/angular-router-empty-paths-componentless-routes
Here's how you can solve this.
Late to answer but If you still have the same issue here's how I solved the same problem..
This is my route
I have defined
myhouse
as parent route and defined a**
child route to it. Parent componentMyParenthouseComponent
holds a<router-outlet></router-outlet>
as template, nothing else.(If you need anything else it's upto you)IN my
MyhouseComponent
ngOnInit
I am doing simply thisimported routes like this
This will get me the current route
For example in case of this route
it will give me
You can easily manipulate this url for your use.
You could use a
UrlMatcher
by providingmatcher
key for aRoute
. Unfortunately it's undocumented for now, so you may need to check the source of router/src/config.ts:This basically allows you to write a function that can do any kind of matching, including regular expressions.
It's still experimental, so there aren't many examples around, but github/matanshukry has kindly provided a gist example of
ComplexUrlMatcher
. It should give you an idea how to implement one that suits your needs (sadly there's no license, so you can't use it as-is).