2017-07-07 52 views
0

我嘗試過使用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?

+0

我已經將問題縮小到了我的服務中的異步問題。一旦完全解決,我會更新我的答案。 – Aluminum91

回答

0

我剛剛在我的應用程序中嘗試過,它似乎工作正常。你是指在開發時運行嗎?或者在部署到生產後? https://github.com/DeborahK/Angular-Routing/tree/master/APM-Final

哦......我只是看着您發佈的URL接近,它看起來像你指的是生產:

,如果你想看看和比較,您可以在這裏下載我的示例代碼。您需要在您的服務器上實施URL重寫以處理這些「深層鏈接」。你怎麼做取決於你使用的服務器類型。

+0

我指的是從CLI開發的時候運行,但是你提出了一個關於URL重寫的非常好的觀點,也需要解決。 我翻看了你的示例項目,它確實應該像你的樣子。我不知道我錯過了什麼。 – Aluminum91

+0

如果你運行我的工作是否像預期的那樣工作?你使用什麼版本的CLI? – DeborahK