const routes: Routes = [
{ path: 'x', component: xComponent },
{ path: 'y', component: yComponent },
{ path: 'zComponent', component: zComponent }
];
如果我写的网址x小,它将把我定向到组件页面,如果我写的是X Capital,它将说无效的网址。
如何使url不区分大小写
最佳答案
两种选择..
1.创建1个URLSerializer类
import { DefaultUrlSerializer, UrlTree } from '@angular/router';
export class LowerCaseUrlSerializer extends DefaultUrlSerializer {
parse(url: string): UrlTree {
return super.parse(url.toLowerCase());
}
}
并在您的app.module.ts中
providers: [
{
provide: UrlSerializer,
useClass: LowerCaseUrlSerializer
}
],
选项2:
路由文件中的简单解决方法。
{ path: '/home', redirectTo: ['/Home'] },
{ path: '/Home', component: HomeComponent, name: 'Home' },
关于angular 2 Routes 3.0,区分大小写,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42065409/