简体   繁体   English

Angular 2路由-隐藏URL

[英]Angular 2 Routing - Hide URL

I know that you can hide the URL when routing using this.router.navigate(["/Pages"], { skipLocationChange: true }); 我知道您可以使用this.router.navigate(["/Pages"], { skipLocationChange: true });路由时隐藏URL this.router.navigate(["/Pages"], { skipLocationChange: true }); but when i use window.open("/Pages") it has the URL. 但是当我使用window.open("/Pages")它具有URL。

Is there any way to hide the URL when using window.open() or a way to use the angular2 router to open the URL in a new tab? 使用window.open()时,是否有任何隐藏URL的方法,或使用angular2路由器在新选项卡中打开URL的方法?

You can inject the Location like this: 您可以这样注入Location

constructor(private readonly location: Location) {
    //...
}

And then in ngOnInit() run this: 然后在ngOnInit()运行:

public ngOnInit(): void {
    this.location.replaceState("/");
}

This replaced the URL in the browser with the URL you specify in replaceState() . 这将浏览器中的URL替换为您在replaceState()指定的URL。

Found an easy way in the end. 最终找到了一种简单的方法。 history.pushState({},"Edit","http://localhost:4200/"); works fine for what im after. 对于之后的工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM