I have created a small application in Angular. I'm trying to make 404 Error Page in that application. If user put any wrong URL then this Error page will be Display automatically.
For that I have done - I have created a Separate Component name as "NotFoundComponent".
NotFoundComponent.html
<h1>Page Not Found !! </h1>
<a routerLink="/">Go back to Home Page</a>
app-routing.module.ts
const routes: Routes = [
{ path: 'about-us', component: AboutUsComponent },
{ path: 'how-it-works', component: HowItWorksComponent },
{ path: '**', component: NotFoundComponent },
];
@NgModule({
imports: [
RouterModule.forRoot(routes)
],
exports: [RouterModule],
//providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}],
})
export class AppRoutingModule { }
app.module.ts
@NgModule({
declarations: [
AppComponent,
AboutUsComponent,
HowItWorksComponent
],
imports: [
BrowserModule,
AuthModule,
UserModule,
PublicModule,
SharedModule,
AppRoutingModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
If I'm putting any wrong URL than it is working as fine, It is showing 404 Error Page. i.e. NotFoundComponent.html.
But my problem is that when my application get load for the first time, it automatically displaying 404 Error Page or NotFoundComponent.html.
Rather than this, it is working fine, I have tried solution of HashLocationStrategy in app-routing.module.ts and but it won't work for me.
From my Point of view, I have set the correct order for Module in app.module.ts file and routing for app-routing.module.ts file.
Any help will be appreciated....Thank u!!