Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page not found handling without changing url Angular 2

I have routes:

    const routes: Routes = [
        { path: '', component: DashboardComponent, canActivate: [RouteGuard, AuthGuard] },
        { path: 'signup', component: SignUpTenantComponent, canActivate: [RouteGuard] },
        { path: 'signin', component: SignInComponent, canActivate: [RouteGuard] },
        { path: ':url', component: DashboardComponent, canActivate: [RouteGuard, AuthGuard] },
        { path: 'page-not-found', component: PageNotFoundComponent },
        { path: '**', redirectTo: 'page-not-found' }
    ];

And I have guard where I check url and if it's wrong then redirect on PageNotfoundComponent:

this.router.navigate(['page-not-found']);

And as a result I see in url localhost:port/page-not-found. But I want to do that without changing url. If I add { skipLocationChange: true } to router.navigate then I will see main url localhost:port/. I want to see wrong url, for example localhost:port/rgqadff with PageNotFoundComponent content.

How can I resolve that?

like image 361
A. Gladkiy Avatar asked Apr 09 '26 22:04

A. Gladkiy


1 Answers

I'm using the below way and it works fine, whatever you write it will show the NotFoundComponent without changing the url

const APP_ROUTES: Routes = [
   {path: '' , component: HomeComponent },
   {path: 'about', component: AboutComponent },
   {path: '**', component: NotFoundComponent }
];
like image 100
baha almomani Avatar answered Apr 12 '26 13:04

baha almomani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!