简体   繁体   English

订阅http发布请求后在router.navigate上出现ObjectUnsubscribedError

[英]ObjectUnsubscribedError on router.navigate inside a subscribe of http post request

I am trying to navigate to a page after I get a response from the http post call. 从http帖子获得回复后,我试图导航至页面。 The router navigate needs the response object since it routes based on the response.id as shown in below code. 路由器导航需要响应对象,因为它基于response.id进行路由,如下面的代码所示。

user.component.ts: user.component.ts:

this._userService.createUser(data).subscribe(response => {
    this.router.navigate(['userEdit/' + response.id]);
});

Inside the service: 服务内部:

createUser(userdata: User): Observable<User> {
    return this._httpClient.post<User>(url, userData, this._httpOptions);
}

Inside app.routing.ts 内部app.routing.ts

{path: 'userEdit/:userId', component: UserEditComponent}

It navigates correctly to the 'userEdit/10' path but on the console it gives ObjectUnsubscribedError. 它可以正确导航到“ userEdit / 10”路径,但在控制台上会显示ObjectUnsubscribedError。

The application is working fine but I want to understand why I am getting this error. 该应用程序运行正常,但我想了解为什么会出现此错误。

Console Error: 控制台错误:

控制台错误的屏幕截图

I realized through the stack trace that the error is thrown by a ngx-bootstrap modal directive. 通过堆栈跟踪,我意识到该错误是由ngx-bootstrap模态指令引发的。

modal.directive.js:276

I was hiding the modal before the createUser function call and due to the router.navigate it was getting destroyed and that was throwing the error. 我在createUser函数调用之前隐藏了模态,并且由于router.navigate被破坏,这引发了错误。

Fixed it by adding the createUser function call inside modal.onHidden.subscribe() 通过在modal.onHidden.subscribe()中添加createUser函数调用来修复此问题

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

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