Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to not return to the page I was redirect from?

I want to have foo facade instance for a foo component:

@Component({
  selector: 'foo',
  template: `in foo component. state: {{state$ | async |json}}`,
  providers: [FooFacade], //<---- instance belong to the component.
})

And FooFacade is load the data from the server at the constructor.

But when the data is not exist I want to redirect to not found page (I defined it by the route):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

load() {
    of([{ id: 1, name: 'foo' }]).subscribe((d) => {
      this.state.next(d);

      // fake, the data is not exist  so I redirect to fake url and angular will redirect by route rule.
      this.router.navigateByUrl('/fake-not-found');
    });
  }

.......
  RouterModule.forRoot([
      { path: 'foo', component: FooComponent },
      { path: '**', component: NotFoundComponent },
    ]),

It’s working as expected.

In not found page, I hit "back" button from the browser I return to the /foo page. and then the redirect happens again.

I want when I redirect to not found page then this page that im on not to be in the route history.

How do I config the route to not return to the foo page?

stackblitz example

please note:
I know I should do it with canActive/resolver but I just can’t do it because I load the data from the service and the instance is not created yet.

>Solution :

I think what you’re looking for is replace url

Look at this:
In Angular, how to remove the current route from history on navigation?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading