2016-09-20 68 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 { }