在 angular 4 中从解析器获取数据后如何更改 URL?

How can i change URL after get data from resolver in angular 4?

我想在从解析器获得博客标题后将 url /blog/:id 更改为 /blog/blog-title。示例:当我导航到 blog/1 并且 ID 为 1 的博客的标题为 how-can-i-make-this 时,url 应该是 /blog/how-can-i-make-this。我如何在 angular 4 中执行此操作?

使用Angular的路由器并使用其导航功能:

打字稿

import {Router} from '@angular/router'; // import the router


constructor(private router:Router){} //put this in your contructor

// call this when you want to change you want to change the url
somefunction() {
  this.router.navigate(['how-can-i-make-this']).then(response => {
    console.log(response);
  });
}

如果您不想重新加载,请查看此答案:

试试下面,

window.history.pushState(null, "Title", "/<blog-title>");

阅读有关 pushState() 的更多信息here