1

I have recently upgraded to Angular2 final release. I don't think this is a duplicate because HashLocationStrategy stopped working for me after the Angular2 Final/RC7 update. Previously, refreshing the page would fetch/GET the related route with a hash (#) and reload the page. Now, I get this error on any refreshed page:

enter image description here

I believe this is happening because it is trying to load http://localhost:3000/main/home instead of http://localhost:3000/#/main/home.

Any idea why HashLocationStrategy stopped working? Should I be importing HashLocationStrategy in my @NgModule?

Community
  • 1
  • 1
jhhoff02
  • 1,179
  • 1
  • 17
  • 24
  • Possible duplicate of http://stackoverflow.com/questions/38265536/angular-2-rc-4-hashlocationstrategy-no-longer-working – Supamiu Sep 20 '16 at 13:53
  • 1
    https://angular.io/docs/ts/latest/guide/router.html#!#-hashlocationstrategy- here is everything you'll need. – Supamiu Sep 20 '16 at 14:08

2 Answers2

4

You should set LocationStrategy to HashLocationStrategy in your AppModule's providers:

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

@NgModule({
    imports: [
        ...
    ],
    declarations: [
        ...
    ],
    bootstrap: [...],
    providers: [
        { provide: LocationStrategy, useClass: HashLocationStrategy }
    ]
})

export class AppModule { }
Stefan Svrkota
  • 48,787
  • 9
  • 98
  • 87
0

I don't know if that is still valid but there is a cleaner way:

import {RouterModule} from '@angular/router';

@NgModule({
    imports: [
        RouterModule.forRoot(ROUTES_ARRAY, {useHash: true})
    ],
    declarations: [
        ...
    ],
    bootstrap: [...],

})

export class AppModule { }
tuncay
  • 111
  • 1
  • 3