我正在使用角度2,谷歌地图等制作房地产网站.当用户更改地图的中心时,我执行搜索到api,指示地图的当前位置以及半径.问题是,我想在不重新加载整个页面的情况下在URL中反映这些值.那可能吗?我找到了一些使用AngularJS 1.x的解决方案,但没有关于angular 2的解决方案.
当我的后端向我发送404错误(URL 有效,只是因为找不到资源,例如http://localhost:4200/post/title-not-exist)时,我希望 Angular 重定向到我的NotFoundComponent,而不更改浏览器中的 URL。
代码如下(简化):
\n\nconstructor(private router: Router, private location: Location) {}\n\nhandleError(err: HttpErrorResponse) {\n switch (err.status) {\n case 404:\n url = this.router.routerState.snapshot.url;\n // \'**\' is set in the app-routing.module.ts\n // { path: \'**\', component: NotFoundComponent }\n this.router.navigate([\'**\'], { replaceUrl: false });\n // weird here too\n // without the setTimeout(), replaceState will not work\n setTimeout(() => {\n this.location.replaceState(url);\n });\n break;\n\n default:\n break;\n }\n return throwError(err);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n现在,我可以重定向到NotFoundComponent并且 URL 没有更改,问题是: