如何从角度4中的解析器获取数据后如何更改URL?

时间:2017-11-06 18:05:34

标签: angular typescript angular2-routing

我想从解析器获取博客标题后,将我的网址/blog/:id更改为/blog/blog-title。示例:当我导航到blog/1并且ID为1的博客标题为how-can-i-make-this时,网址应为/blog/how-can-i-make-this。我怎么能在角4中做到这一点?

2 个答案:

答案 0 :(得分:0)

使用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);
  });
}

如果您不想重新加载,请参阅以下答案: Change route params without reloading in angular 2

答案 1 :(得分:0)

尝试以下,

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

详细了解pushState()here

希望这会有所帮助!!