使路径不区分大小写

使路径不区分大小写

本文介绍了Angular2:使路径不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下路由配置.

@RouteConfig([
    {
        path: '/home',
        name: 'Homepage',
        component: HomepageComponent,
        useAsDefault: true
    }
)
export class AppComponent {
}

无论何时将浏览器指向/home,此路由均有效,但不适用于/Home或任何其他情况下的变体.我该如何使路由器路由到组件而不用担心这种情况.

whenever the browser is pointed to /home this route works but not for /Home or any other case variations. How can I make the router to route to the component without caring the case.

谢谢

推荐答案

这就是我所做的.

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

export class LowerCaseUrlSerializer extends DefaultUrlSerializer {
    parse(url: string): UrlTree {
        // Optional Step: Do some stuff with the url if needed.

        // If you lower it in the optional step
        // you don't need to use "toLowerCase"
        // when you pass it down to the next function
        return super.parse(url.toLowerCase());
    }
}

还有

@NgModule({
    imports: [
      ...
    ],
    declarations: [AppComponent],
    providers: [
        {
            provide: UrlSerializer,
            useClass: LowerCaseUrlSerializer
        }
    ],
    bootstrap: [AppComponent]
})

这篇关于Angular2:使路径不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 00:24