Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 2 Routes 3.0, case sensitive

const routes: Routes = [
 { path: 'x', component: xComponent }, 
 { path: 'y', component: yComponent }, 
 { path: 'zComponent', component: zComponent }
];

If I write in the url x small it will direct me to the component page, if I write X Capital it will say not valid url.

How to make the url insensitive Case

like image 702
Mohamed Fathy Avatar asked Feb 06 '17 10:02

Mohamed Fathy


People also ask

Are routes case sensitive in Angular?

The routes in Angular are case sensitive. In the routeconfig, you have specified the url path as "product".

Which of the following makes route path case sensitive in angular routes?

That's because the routes that are configured using UI-Router are case sensitive. To make route insensitive, all you have to do is to inject $urlMatcherFactoryProvider service into the config() function and call caseInsensitive(true) function.

Can we have multiple routes in angular?

Angular Router supports multiple outlets in the same application. A component has one associated primary route and can have auxiliary routes. Auxiliary routes enable developers to navigate multiple routes at the same time.


1 Answers

Two options .. 1. create 1 URLSerializer Class

import { DefaultUrlSerializer, UrlTree } from '@angular/router';

export class LowerCaseUrlSerializer extends DefaultUrlSerializer {
    parse(url: string): UrlTree {
        return super.parse(url.toLowerCase());
    }
}

And in your app.module.ts

providers: [
        {
            provide: UrlSerializer,
            useClass: LowerCaseUrlSerializer
        }
    ],

Option 2: Simple workaround in your route file.

{ path: '/home', redirectTo: ['/Home'] },
{ path: '/Home', component: HomeComponent, name: 'Home' },
like image 140
Parth Ghiya Avatar answered Oct 14 '22 21:10

Parth Ghiya