相关疑难解决方法(0)

更改路线参数而无需以角度2重新加载

我正在使用角度2,谷歌地图等制作房地产网站.当用户更改地图的中心时,我执行搜索到api,指示地图的当前位置以及半径.问题是,我想在不重新加载整个页面的情况下在URL中反映这些值.那可能吗?我找到了一些使用AngularJS 1.x的解决方案,但没有关于angular 2的解决方案.

javascript routes angular2-routing angular

91
推荐指数
8
解决办法
9万
查看次数

重定向到 404 组件而不更改浏览器 URL,并使历史记录在 Angular 中正常运行

当我的后端向我发送404错误(URL 有效,只是因为找不到资源,例如http://localhost:4200/post/title-not-exist)时,我希望 Angular 重定向到我的NotFoundComponent,而不更改浏览器中的 URL。

\n\n

代码如下(简化):

\n\n
constructor(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}\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在,我可以重定向到NotFoundComponent并且 URL 没有更改,问题是:

\n\n …

javascript routes typescript angular

2
推荐指数
1
解决办法
1796
查看次数