Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Routing Does Not Work When Deployed to Http Server

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 component ' command. Then I specified routing in the app-routing.module.ts as following.

import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; import { UserComponent } from './user/user.component'; import { ErrorComponent } from './error/error.component'; import { SpecialpageComponent } from './specialpage/specialpage.component';  const routes: Routes = [   {     path: '',     component: HomeComponent   },   {     path: 'about',     component: AboutComponent   },     {     path: 'user',     component: UserComponent   },   {     path: 'specialpage',     component: SpecialpageComponent   },   {     path: '**',     component: ErrorComponent   }  ];  @NgModule({   imports: [RouterModule.forRoot(routes)],   exports: [RouterModule],   providers: [] }) export class AppRoutingModule { } 

app.module.ts is as following.

 import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { HttpModule } from '@angular/http'; import { AppRoutingModule } from './app-routing.module';  import { AppComponent } from './app.component'; import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; import { ErrorComponent } from './error/error.component'; import { UserComponent } from './user/user.component'; import { SpecialpageComponent } from './specialpage/specialpage.component';  @NgModule({   declarations: [     AppComponent,     HomeComponent,     AboutComponent,     ErrorComponent,     UserComponent,     SpecialpageComponent   ],   imports: [     BrowserModule,     FormsModule,     HttpModule,     AppRoutingModule   ],   providers: [],   bootstrap: [AppComponent] }) export class AppModule { } 

I have not added any modifications for the other components. Then I deployed the application using 'ng serve' command and the app works fine with the links. Eg: http://localhost:4200/about

enter image description here

But when I deploy the project in http-server, the links do not work as expected. I deployed the app using 'http-server ./dist' command and the app gets deployed fine, but the links do not work. When I go to 'http://localhost:4200/about', it gives 404 error.

enter image description here

Am I doing anything wrong? Why 'ng-serve' works and 'http-server' does not work?

Any help would be appreciated. Thanks in advance.

I have uploaded my project to github.

like image 324
kinath_ru Avatar asked Apr 21 '17 06:04

kinath_ru


People also ask

How do I enable Angular routing?

Add the AppRoutingModule link. In Angular, the best practice is to load and configure the router in a separate, top-level module. The router is dedicated to routing and imported by the root AppModule . By convention, the module class name is AppRoutingModule and it belongs in the app-routing.

Do I need a routing module always in Angular?

No, the Routing Module is a design choice. You can skip routing Module (for example, AppRoutingModule) when the configuration is simple and merge the routing configuration directly into the companion module (for example, AppModule).

What is Pathlocationstrategy?

A LocationStrategy used to configure the Location service to represent its state in the path of the browser's URL.

What is hash location strategy in Angular?

A LocationStrategy used to configure the Location service to represent its state in the hash fragment of the browser's URL.


1 Answers

This problem is solved by implementing HashLocationStrategy which adds # to all your routes. You achieve this by adding HashLocationStrategy to AppModule providers.

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

and add the corresponding import

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

This will solve your problem.

And if you don't want to use the HashLocationStrategy, you can use the PathLocationStrategy, and so your Angular app will not show Hash in the URL.

like image 115
Manu Avatar answered Oct 16 '22 16:10

Manu