我嘗試過使用canActivate和canLoad在兩個不同的模塊級別,我仍然無法直接訪問延遲加載模塊。Angular 4延遲加載模塊canLoad或canActivate後衛直接url/
在app.routing.module.ts
import { AuthGuardImpl } from './lazy-load/auth-guard.service';
import {AuthGuard} from './lazy-load/auth.service';
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'lazy', canLoad: [AuthGuardImpl], loadChildren: 'app/lazy-load/lazy-load.module#LazyLoadModule'},
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [
CommonModule,
RouterModule.forRoot(routes)
],
exports: [RouterModule],
declarations: [],
providers: [AuthGuardImpl, AuthGuardService]
})
在哪裏,如果你不直接導航到http://url.com/lazy
上面的代碼工作。但去http://url.com/
然後遵循路由器鏈接到懶惰模塊。
同樣地,我嘗試:
import { AuthGuard } from './auth.service';
import { AuthGuardImpl } from './auth-guard.service';
const lazyRoutes: Routes = [
{ path: '', component: LazyLoadComponent, canActivate: [AuthGuardImpl],
children: [
{ path: 'dashboard', loadChildren: 'app/lazy-load/dashboard/dashboard.module#DashboardModule' },
{ path: '', redirectTo: 'dashboard', pathMatch: 'full'},
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(lazyRoutes)],
exports: [RouterModule],
declarations: [],
providers: [AuthGuardImpl, AuthGuard]
})
再次,不工作直接去http://url.com/lazy
,但如果應用程序加載會工作,然後用戶通過routerlink導航到/lazy
。
是否可以導航到使用警衛的延遲加載模塊的直接url?
我已經將問題縮小到了我的服務中的異步問題。一旦完全解決,我會更新我的答案。 – Aluminum91