如何处理网址上的重复正斜杠

时间:2019-12-03 17:32:29

标签: javascript reactjs

我正在使用import { Component, OnInit } from '@angular/core'; import { BankService } from '../bank.service'; import { Bank } from '../bank'; import { FormGroup, FormControl } from '@angular/forms'; @Component({ selector: 'app-add-customer', templateUrl: './add-customer.component.html', styleUrls: ['./add-customer.component.css'] }) export class AddCustomerComponent implements OnInit { constructor(private bankService:BankService) { } bank:Bank=new Bank(); submitted=false; ngOnInit() { this.submitted=false; } banksaveform=new FormGroup({ bankcustid:new FormControl(), bankcustname:new FormControl(), bankcustbalance:new FormControl(), bankcustpassword:new FormControl() }) saveCustomer(saveCustomer) { this.bank=new Bank(); this.bank.cust_id=this.banksaveform.get("bankcustid").value; this.bank.cust_name=this.banksaveform.get("bankcustname").value; this.bank.cust_balance=this.banksaveform.get("bankcustbalance").value; this.bank.cust_password=this.banksaveform.get("bankcustpassword").value; this.submitted=true; this.save(); } save() { this.bankService.addCustomer(this.bank) .subscribe(data=>console.log(data),error=>console.log(error)); this.bank=new Bank(); } addCustomerForm() { this.submitted=false; this.banksaveform.reset(); } } ,并且试图在类似于Facebook的网站上处理重复的正斜杠。其工作方式如下:

如果用户访问权限:React,则网址已更新为https://facebook.com///settings

我正试图像这样覆盖https://facebook.com/settings

window.location.pathname

但是它会在窗口上返回无限的刷新循环。

那么,我该怎么做?有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您需要留下一个斜线:

let pathName = window.location.pathname;
const pattern = /\/+[/]/;
const urlFinal = pathname.replace(pattern, "/");
if (urlFinal.length < pathName) window.location.pathname = urlFinal;