M
M
mletov2018-09-21 09:04:20
Angular
mletov, 2018-09-21 09:04:20

How to refresh page with Get parameter via router?

Please, prompt:
There is a project on Angular 2+, SPA.
There are entities, comments are attached to each entity.
The comment page for each entity has the /comment-list/123 address in the router, where 123 is the entity id.
On the same page there is a form for adding comments, after adding a comment, the list of comments on the page is supposed to be updated.
Tried to reload via

this.commentService.createComment(this.comment).subscribe(data => {
    var id = Number.parseInt(activeRoute.snapshot.params["id"]);
    this.router.navigate(['/comment-list', id]);
});

and
this.commentService.createComment(this.comment).subscribe(data => {
    var id = Number.parseInt(activeRoute.snapshot.params["id"]);
    this.router.navigateByUrl("/comment-list/" + id)
});

But either the page address does not change, or it changes, but the list of comments is not updated (the added comment does not appear).
For now I'm using window.location.reload() , but it's somehow not SPA-wise.
Address on the router
const appRoutes: Routes = [
   ...
    { path: 'comment-list/:id', component: CommentListComponent },
  ...
];

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2018-09-21
@streetflush

Reloading the page is not SPAshny at all)
You get data from the service, so tell him to re-request comments and not fight with the router.

S
Sergey, 2018-09-22
@Sergamers

There is a service in which a get and post request is made. There is a component which communicates with this service. In the component is done: It is
possible by handicraft

this.postRequest().subscribe( () => {
  this.getRequest().subscibe( () => {
    // ToDo
  });
})

Or Make processing in post-e:
this.postRequest().subscribe( () => {
 // ToDo
});

//----------
// Сервис
postRequest(...data) {
 this.http.post(...).pipe(
  switchMap( () => { // меняем поток на getRequest
   return this.getRequest();
  })
 )
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question