SoFunction
Updated on 2025-04-08

Solve the problem of refreshing 404 on the Angular4 project to be deployed to the server

I just encountered the Angular4 project npm run build and deployed to the server to access it, but the page refresh will have an error of 404! I searched online and saw some people’s solutions and errors in it. I could tell at a glance that these people really have problems. I decided to write a blog. Why is it so uncareful? If you write the wrong ones, it will harm others!!

Solution to solve the error of angular2 page reporting 404 after refreshing:

Configuration

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

@NgModule({
 declarations: [AppCmp],
 bootstrap: [AppCmp],
 imports: [BrowserModule, routes],
 providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}]
});

Main code added:

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

After setting this way, when accessing the angular site, a # anchor point will be automatically added after the root node. If you refresh again, you will not report a 404 error.

The above article solves the problem of resizing the Angular4 project to the server and refresh 404 is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.