代码之家  ›  专栏  ›  技术社区  ›  Sunil Singh

为什么必须为每个Lazy模块提供HTTP_拦截器

  •  0
  • Sunil Singh  · 技术社区  · 6 年前

    我有 AuthInterceptor 提供于 AppModule . 它在 应用程序模块 然而,当它涉及到 LazyModule interceptor 所有懒惰模块都能工作。

    我真的很惊讶为什么会这样。我已经在其他项目中实现了相同的东西,但在这里我必须在每个模块中提供。不确定是什么导致了这个问题。

    应用程序模块

    @NgModule({
        declarations: [AppComponent],
        imports: [
            BrowserModule,
            BrowserAnimationsModule,
            HttpClientModule,
            AppRoutingModule,
    
            //NgbModule.forRoot(),
            ThemeModule.forRoot(),
            CoreModule.forRoot(),
        ],
        bootstrap: [AppComponent],
        providers: [
            {provide: APP_BASE_HREF, useValue: '/'},
            {provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true},
            AuthGuard,
            {
                provide: NbRoleProvider,
                useClass: RoleProvider,
            },
            ZtLoaderService
        ],
    })
    export class AppModule {
    }
    

    批准模块

    const routes: Routes = [
        {
            path: 'pages',
            canActivate: [AuthGuard], // here we tell Angular to check the access with our AuthGuard
            loadChildren: 'app/pages/pages.module#PagesModule'
        },
        {path: '', redirectTo: 'pages', pathMatch: 'full'},
        {path: '**', redirectTo: 'pages'},
    ];
    
    const config: ExtraOptions = {
        useHash: true,
    };
    
    @NgModule({
        imports: [RouterModule.forRoot(routes, config)],
        exports: [RouterModule],
    })
    export class AppRoutingModule {
    }
    

    页面模块

    @NgModule({
      imports: [
        PagesRoutingModule,
        ThemeModule,
        Dashboard2Module
      ],
      declarations: [
        PagesComponent,
      ],
    })
    export class PagesModule {
    }
    

    const routes: Routes = [{
        path: '',
        component: PagesComponent,
        children: [{
            path: 'dashboard',
            component: DashboardComponent2,
        },
        {
            path: 'profile',
            loadChildren: './profile/profile.module#ProfileModule',
    
        }],
    }];
    
    @NgModule({
        imports: [RouterModule.forChild(routes)],
        exports: [RouterModule],
    })
    export class PagesRoutingModule {
    }
    

    档案模块

    @NgModule({
        imports: [
            ThemeModule,
            ProfileRoutingModule,
            Ng2SmartTableModule,
    
        ],
        declarations: [
            ...routedComponents,
            ...components
    
        ],
         providers: [{provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true}],   //<--- Interceptor is provided here. Could be avoided.
    })
    export class ProfileModule {}
    
    0 回复  |  直到 6 年前
    推荐文章