Angular 角度2:如何使用replaceState更改激活的路由URL

Angular 角度2:如何使用replaceState更改激活的路由URL,angular,routes,angular-ui-router,Angular,Routes,Angular Ui Router,在与replaceState一起使用时,我遇到了一些与激活路由相关的问题。我试图做的是,在提交表单之后,我将用得到的响应替换路由URL中的keyId和id 让我在这里解释一下。下面是我激活的路由URL: home/screen/user/detail/keyId/id 在此视图下,我使用以下方法提交我的表格: onSubmit(userItem: UserItem){ this.userService.saveUser(userItem).subscribe(res => {

在与
replaceState
一起使用时,我遇到了一些与激活路由相关的问题。我试图做的是,在提交
表单之后,我将用得到的响应替换路由URL中的
keyId
id

让我在这里解释一下。下面是我激活的路由URL:

home/screen/user/detail/keyId/id
在此视图下,我使用以下方法提交我的表格:

onSubmit(userItem: UserItem){ 
     this.userService.saveUser(userItem).subscribe(res => {
     let keyId = res.keyId;
     let id = res.id;

     // tried this but doesn't work
     /* this.router.navigate(["home/screen/user/detail/" + keyId + "/" + id], { replaceUrl: true }) */

     // this one working but seems like temporary fix
     this.location.replaceState("home/screen/user/detail/" + keyId + "/" + id);
   })
  }
上面的代码将路由URL替换为新的
keyId
id
,但是如果我刷新页面,它会给出一个错误,比如无法匹配任何路由。但我的要求是,一旦我刷新页面,它应该加载更改后的URL


我尝试了
router.navigate
而不是
replaceState
,但它没有按预期工作。如果有人能在这种情况下帮助我,我将非常感谢。

您可以使用以下代码来实现这一点:

 this._router.navigate(['YourUrl/UpdatedKeyId/id']);
 window.location.reload();

您可以使用以下代码来实现此功能:

 this._router.navigate(['YourUrl/UpdatedKeyId/id']);
 window.location.reload();

也适用于角度2+(即角度9

你试过这个吗

import { Location } from '@angular/common';

constructor(private location: Location) { }

this.location.replaceState('/your-url/blah/blah');
以上内容摘自以下内容

希望这对某人有帮助:)


谢谢

也适用于Angular 2+(即Angular 9

你试过这个吗

import { Location } from '@angular/common';

constructor(private location: Location) { }

this.location.replaceState('/your-url/blah/blah');
以上内容摘自以下内容

希望这对某人有帮助:)

多谢各位