0

When I start my app (localhost/), everything works fine and it redirects to the default route (localhost/welcome). However, when I reload the page, IIS is looking for a directory welcome, which doesn't exist, because it's just a virtual route.

I read that there are two solutions. 1. IIS Url Rewrite, 2. Using the HashLocationStrategy (no idea why and how this solves the problem).

Unfortunately, I cannot get solution 2 to work:

    @NgModule({
    imports:      [ MapModule, BrowserModule, HttpModule, FormsModule, routing, DataTableModule, MdGridListModule, GrowlModule, TabMenuModule, PanelModule],
    declarations: [ Application, CellDetailComponent, WelcomeComponent, SlimLoadingBarComponent],
    bootstrap:    [ Application ],
    providers: [
        appRoutingProviders,
        AppConfig,
        { provide: APP_INITIALIZER, useFactory: (config: AppConfig) => () => config.load(), deps: [AppConfig], multi: true },
        { provide: LocationStrategy, useClass: HashLocationStrategy}
    ]
})

The route setup:

import { Routes, RouterModule } from '@angular/router';
import {CellDetailComponent} from "./components/infopanel/cell-detail.component";
import {WelcomeComponent} from "./components/infopanel/welcome.component";
import { ModuleWithProviders } from '@angular/core';

const appRoutes: Routes = [
    { path: 'welcome', component: WelcomeComponent },
    { path: 'cell/:id', component: CellDetailComponent },
    { path: '',  redirectTo: '/welcome', pathMatch: 'full'},        // default route
    { path: '**', component: WelcomeComponent }                     // PathNotFound route
];

export const appRoutingProviders: any[] = [

];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

What am I doing wrong?

user66875
  • 2,772
  • 3
  • 29
  • 55
  • 1
    Possible duplicate of [Angular 2 : 404 error occur when i refresh through Browser](http://stackoverflow.com/questions/35284988/angular-2-404-error-occur-when-i-refresh-through-browser) – user66875 Jan 18 '17 at 09:20

2 Answers2

1

Just found the answer:

    RouterModule.forRoot(routes, { useHash: true })
user66875
  • 2,772
  • 3
  • 29
  • 55
0

In the latest version of angular 4 we don't have separate bootstrap[AppComponent] you just need to write:

RouterModule.forRoot(routes, **{ useHash: true }**)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Er. Bahuguna Goyal
  • 192
  • 1
  • 3
  • 10