javascript - FEL Fel: Oavbrutet (i löfte): Fel: Kan inte matcha några rutter. URL-segment: '2017-18'


Att ha runda parenteser ( ) i några av URL:erna exempelvis, http://localhost:4200/pdf/abc/AQAR%20(2017-18).pdf eller http://localhost:4200/pdf/abc/AQAR%20(2016-17).pdf

punkt:- ta slug från URL:er

medan omdirigering sker uppstår ett fel nedan är källkoden

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() {
  }
}

när jag lägger in nedanstående URL får jag ett fel

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'

om jag klistrar in följande URL i URL-fältet fungerar det och PDF:en kommer att visas https://testexample/pdf/abc/AQAR%20(2017-18).pdf

kan någon säga mig var jag gör fel

Svar #1

Du måste undkomma '(' med %28 och ')' med @29 och '.' med %2e

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

Du kan hänvisa till https://www.eso.org/~ndelmott/url_encode.html

Svar #2

Vänligen koda filnamnet innan du navigerar, ditt problem kommer att lösas!

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