我尝试过使用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