I am going to develop a simple Angular 2 application. I have created a project with routing, using Angular CLI and added several components to the app using \'ng generate co
export const routes: Routes =[
**{ path: '', redirectTo: '/', pathMatch: 'full'},**
{ path: 'about', component: AboutComponent},
{ path: 'user', component: UserComponent},
{ path: 'specialpage', component: SpecialpageComponent},
{ path: '**', component: ErrorComponent}
];
Look at the content of blockquote.
And then you give the name of "HttpModule" at providers:[]
in app.module.ts
Thanks.
I also faced this issue and found a solution that does not require HashLocationStrategy solution.
Issue is, a server like tomcat looks for a actual folder (for eg : /about ) which does not exist in angular app, as its a single page application. So every request needs to be redirected to index.html.
Easiest way to fix this issue is to add the following java class and override addResourceHandlers() method like below:
@Configuration
class WebMVCCustomConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:static").resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String resourcePath, Resource location) {
String path = "static/";
path += (resourcePath.contains(".") && !resourcePath.equals("index.html")) ? resourcePath : "index.html";
Resource resource = new ClassPathResource(path);
return resource.exists() ? resource : null;
}
});
}
}
This will fix all the issues and this solution does not exist anywhere.
The exact reason why this particular issue is being faced is because of your server settings.
So when you implement the application there are certain steps that you will have to take. One of the essential steps is to mark a particular segment of your path say: http://domain-name/main where main being the path segment must be used as the identifier in your server settings say your server.js or app.js when it comes to a node backend or a webconfig file for IIS and Tomcat deployment.
The reason for marking a particular segment is so that when the server receives any request with that particular segment + additional you reroute it to your application in www or public folder for the angular router to kick in.
This process is known as url rewriting. Hence if the web server or the app server depending on the application size is not under your control then please use hashLocationStratergy else it is always neat to use pathLocationStartergy as it is helpful when it comes to history tracking and other aesthetic and performance benefits.
To read more on this you can visit these links:
For IIS: https://blog.angularindepth.com/deploy-an-angular-application-to-iis-60a0897742e7
I've resolved this problem with adding this in AppModule Providers:
providers: [{provide: LocationStrategy, useClass: PathLocationStrategy}]
and importing
import { PathLocationStrategy, LocationStrategy } from '@angular/common';
Then I created .htaccess:
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
Everything works great without # in URLs :)
You can do it while registering your RouterModule.forRoot, you can pass a second object with propert {useHash: true} like the below :
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {AppComponent} from './app.component';
import {appRoutes} from './app.routes';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
RouterModule.forRoot(appRoutes , {useHash: true})],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
in the project folder create .htaccess file an then write
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
follow this link: https://angular.io/guide/deployment