简体   繁体   English

Angular 未解析编码查询参数的路由

[英]Angular Route with encoded query params not being resolved

In my Angular 10 application I have a route like this在我的 Angular 10 应用程序中,我有这样的路线

http://localhost:4200/employee/enrollments?number=189930097&city=Chicago

Sometimes the URL is being encoded as有时 URL 被编码为

http://localhost:4200/employee/enrollments%3Fnumber%3D189930097%26city=Chicago

and router fails to find a match.并且路由器找不到匹配项。 Is there a way to fix this decoding issue and make it resolve always?有没有办法解决这个解码问题并让它总是解决?

Update: I added my footer component in which I am using routerLink that updates the current URL更新:我添加了我正在使用 routerLink 的页脚组件,它更新了当前的 URL

EmployeeRoutingModule :员工路由模块

export const enrollmentManagementRoutes: Routes = [
    {
        path: 'enrollments',
        component: EnrollmentSearchComponent,
        canActivate: [EmployeeAuthGuardService],
    }
];


@NgModule(
    {
        imports: [
            RouterModule.forChild(enrollmentManagementRoutes)
        ],
        exports: [
            RouterModule
        ]
    })
export class EmployeeRoutingModule
{

}

FooterComponent页脚组件

import {Component, OnInit} from '@angular/core';
import {NavigationEnd, Router} from '@angular/router';
import {environment} from '../../../environments/environment';

@Component({
    selector: 'app-footer',
    templateUrl: './footer.component.html',
    styleUrls: ['./footer.component.scss']
})
export class FooterComponent implements OnInit
{
    appVersion: any;
    currentUrl='/';

    constructor(private router: Router)
    {
    }

    ngOnInit()
    {
        this.appVersion = environment.VERSION;

        //Update Need Assistance link URL, this prevents default URL being '/'
        this.router.events.subscribe(data=>
        {
            if(data instanceof NavigationEnd)
            {
                this.currentUrl=data.url+'';
            }
        });
    }

    navigateByUrl()
    {
        this.router.navigateByUrl(this.currentUrl);
    }
}

Footer Component HTML:页脚组件 HTML:

<a class=" col-sm-12 col-xs-12 col-md-auto request-help-link" id="request-help-link" rel="noopener noreferrer"
               [routerLink]="currentUrl"  style="font-size: 20px" >
                Need Assistance? Click here
            </a>

Would it not make more sense to use a route parameter than a query string?使用路由参数比使用查询字符串更有意义吗?

export const enrollmentManagementRoutes: Routes = [
    {
        path: 'enrollments:number',
        component: EnrollmentSearchComponent,
        canActivate: [EmployeeAuthGuardService],
    }
];

and then route to http://localhost:4200/employee/enrollments/189930097然后路由到http://localhost:4200/employee/enrollments/189930097

and in your component you can use the ActivatedRoute service to get the param.在您的组件中,您可以使用 ActivatedRoute 服务来获取参数。

https://angular.io/api/router/ActivatedRoute https://angular.io/api/router/ActivatedRoute

Have you tried to use something like custom serializer?您是否尝试过使用自定义序列化程序之类的东西?

serializer.ts

export class CustomUrlSerializer implements UrlSerializer {
  parse(url: any): UrlTree {
    const dus = new DefaultUrlSerializer();
    return dus.parse(url);
  }

  serialize(tree: UrlTree): any {
    const dus = new DefaultUrlSerializer();
    const path = dus.serialize(tree);
    // use your regex to replace as per your requirement.
    path.replace(/%3F/g, '?');
    path.replace(/%3D/g, '=');
    return path;
  }
}

and then in App module然后在 App 模块中

...
providers: [
    {provide: UrlSerializer, useClass: CustomUrlSerializer}
  ],
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM