使路由路径不区分大小写

使路由路径不区分大小写

本文介绍了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