【发布时间】:2018-01-12 14:14:24
【问题描述】:
我的 app.routes 中有 4 条路线。
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
export const pageRoutes: Routes = [
{path: '', redirectTo: 'home', pathMatch: 'full'},
{path: 'home', component: HomeComponent},
{path: 'transaction', loadChildren: './app/transaction.module#TransactionModule'},
{path: 'evidence', loadChildren: './app/evidence.module#EvidenceModule'}
];
@NgModule({
imports: [RouterModule.forRoot(pageRoutes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
在 app.routes 中,我没有对 LocationStrategy 进行任何特定设置。因为对于transaction 路由,我想使用angular2 默认的PathLocationStrategy,它不允许用户刷新页面。
但是对于evidence 路由,我实际上希望用户能够刷新页面。所以我想在这里使用 HashLocationStrategy 。
这是evidence-routing.module
@NgModule({
imports: [RouterModule.forChild([
{path:':sessionId', component: EvidenceComponent}
{ path: '**', redirectTo: '/404' },
{ path: '404', component: PageNotFoundComponent}
])],
exports: [RouterModule],
providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}]
})
export class EvidenceRoutingModule {}
我想在 evidence-routing.module 中添加 providers: [{ provide: LocationStrategy, useClass: HashLocationStrategy }] 以启用 HashLocationStrategy 并且仅适用于该路由。
但是一旦我把它放在那里,整个应用程序将采用 HashLocationStrategy,它也适用于事务路由。
我找不到任何好的解决方案来处理这个问题。
对这个问题有什么建议吗?
非常感谢!
【问题讨论】:
标签: angular angular2-routing angular2-services angular-routing angular-router