简体   繁体   中英

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: '2017-18'

Having round parenthesis ( ) in some of urls example, http://localhost:4200/pdf/abc/AQAR%20(2017-18).pdf or http://localhost:4200/pdf/abc/AQAR%20(2016-17).pdf

point:- taking slug from urls

while doing redirection error happen below is the src code

app-routing.modules.ts

{ path: 'pdf/abc/:slug', component: PdfComponent},

pdf.component.ts

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from "@angular/router";

@Component({
 selector: 'app-pdf',
 templateUrl: './pdf.component.html',
 styleUrls: ['./pdf.component.css']
})

export class PdfComponent implements OnInit {

constructor(private route: ActivatedRoute) {
this.route.params.subscribe(params => {
  const slugData = params.slug;
  const URL = 'https://testexample.com/pdf/abc';
  setTimeout(() => {
    if (slugData) {
      window.location.href = URL + slugData;
    } else {
      window.location.href = 'https://testexample.com/';
    }
  }, 0);
 });
}
ngOnInit() {
  }
}

when i put below url its give an error

http://localhost:4200/pdf/abc/AQAR%20(2017-18).pdf

错误

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: '2017-18'
Error: Cannot match any routes. URL Segment: '2017-18'

if i paste below url in url-bar it works and pdf will be shown https://testexample/pdf/abc/AQAR%20(2017-18).pdf 工作网址

can anyone tell me where i am making mistake

Please encode the filename before navigating, your issue will be resolved!

const pdf = 'AQAR%20(2017-18).pdf';
const uri = 'http://test.com/foo?hello=' + encodeURIComponent(pdf);

You have to escape '(' with %28 and ')' with @29 and '.' with %2e

http://localhost:4200/pdf/abc/AQAR%20%282017-18%29%2epdf
http://localhost:4200/pdf/abc/AQAR%20@282016-17%29%2epdf

在此处输入图像描述

You can refer https://www.eso.org/~ndelmott/url_encode.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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