While reloading the page I got 404 error for angular.

 

   I am using angular application; I have no problem in running on localhost when I hosted my application on server. When I clicked the employee button it loads the employee details. However I refreshed the same page, It gives me 404 file or directory not found error.




                I have fixed the issue using routing strategies(HashLocationStrategy) approach. This will make the links look like http://domain.com/#/page for the url  http://domain.com/page.

In app.module.ts, we need to import HashLocationStrategy, LocationStrategyAnd then in NgModule Provider add {{provide: LocationStrategy, useClass: HashLocationStrategy}}.




App.Module.Ts


import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

import { HashLocationStrategy, LocationStrategy } from '@angular/common';

 

@NgModule({

declarations: [

  AppComponent,

 

],

  imports: [

    BrowserModule,

    AppRoutingModule,

    HttpClientModule,

  ],

  providers: [SharedService,{ provide: LocationStrategy, useClass: HashLocationStrategy}],

  bootstrap: [AppComponent]

})

export class AppModule { }


Comments

Popular posts from this blog

Angular Best Practices and Coding Standards: A Guide for Developers

How to SSH AWS EC2 instance?

Top 7 ways to use Interceptors in Angular