2016-01-23 49 views
0

我正在尝试教程Angular2教程,但它似乎并不了解刷新后的路线。 的源代码可以在https://angular.io/resources/live-examples/tutorial/ts/plnkr.htmlAngular2路由如何去基地目录

@Component({ 
    selector: 'my-app', 
    template: ` 
    <h1>{{title}}</h1> 
    <a [routerLink]="['Dashboard']">Dashboard</a> 
    <a [routerLink]="['Heroes']">Heroes</a> 
    <router-outlet></router-outlet> 
    `, 
    styleUrls: ['app/app.component.css'], 
    directives: [ROUTER_DIRECTIVES], 
    providers: [HeroService] 
}) 
@RouteConfig([ 
    {path: '/dashboard', name: 'Dashboard', component: DashboardComponent, useAsDefault: true}, 
    {path: '/heroes', name: 'Heroes', component: HeroesComponent}, 
    {path: '/detail/:id', name: 'HeroDetail', component: HeroDetailComponent} 
]) 

它所做的是,它通过/dashboard取代基/但是当你刷新页面,我希望它明白地发现/dashboard === /dashboard而不是/,因为它认为它是基本目录,所以链接显示/dashboard/dashboard,并且ts无法加载,因为它们是以相对方式包含的。

回答

1

你需要一台服务器,能够通过将follogin您bootstrap()

bootstrap(AppComponent[ 
    ROUTER_PROVIDERS, 
    /* after ROUTER_PROVIDERS */ 
    provide(LocationStrategy, {useClass: HashLocationStrategy}). 
]); 

参见Is Angular 2's Router broken when using HTML5 routes?

+0

由于应对HTML5 pushState的
或更改PathLocationStrategy位置的策略HashLocationStrategy @GünterZöchbauer。我还没有解决我的问题,但它与Angular没有关系,但是Go,所以我会重新说明我的问题。 – Shadowbob

+0

您的服务器需要将所有请求重定向到index.html,同样在这里解释http://stackoverflow.com/questions/31415052/angular-2-0-router-not-working-on-reloading-the-browser –