2016-09-20 63 views
1

我最近升級到了Angular2最終版本。我不認爲這是重複的,因爲HashLocationStrategy在Angular2 Final/RC7更新後停止工作。以前,刷新頁面會使用散列(#)獲取/獲取相關路由並重新加載頁面。現在,我得到任何刷新頁面上此錯誤:Angular2 Final:頁面刷新錯誤,'無法GET'路線。 HashLocationStrategy失敗

enter image description here

我相信這一切,因爲它試圖加載http://localhost:3000/main/home而不是http://localhost:3000/#/main/home

任何想法爲什麼HashLocationStrategy停止工作?我應該在我的@NgModule中輸入HashLocationStrategy嗎?

+0

的可能的複製http://stackoverflow.com/questions/38265536/angular-2-rc-4-hashlocationstrategy-no-longer-working – Supamiu

+1

https://angular.io/docs/ts/latest/guide/router.html#!# -hashlocationstrategy-這是你需要的一切。 – Supamiu

回答

3

您應該設置LocationStrategyHashLocationStrategyAppModule的供應商:

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

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

export class AppModule { } 
+1

非常感謝,這是我失蹤 – jhhoff02

0

我不知道這是否仍然有效,但有一個更清潔的方式:

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

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

}) 

export class AppModule { }