在Angular 6中如何使大小写不敏感的网址格式? [英] In Angular 6 how make case insensitive url pattern?

查看:62
本文介绍了在Angular 6中如何使大小写不敏感的网址格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就我而言,我希望以不区分大小写的方式支持相同的网址.

In my case I want to support same url in case insensitive manner.

示例:它应该支持所有网址

Example: it should support all url

localhost:1029/documentation
localhost:1029/DOCUMENTATION
localhost:1029/DOCUMENTAtion
localhost:1029/docuMENTATION

推荐答案

您应将此提供语句添加到app.module.ts

You should add this provide statement to the app.module.ts

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]
})

这篇关于在Angular 6中如何使大小写不敏感的网址格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆