简体   繁体   English

角延迟加载模块401

[英]Angular lazy loaded module 401

I'm developing an angular app with multiple lazy loaded modules using angular cli. 我正在使用angular cli开发具有多个延迟加载模块的angular应用程序。 The resources (js, css and html) requires authorization which is set by a cookie. 资源(js,css和html)需要由cookie设置的授权。 If the user is not signed in when loading the first page he will be redirected to a SSO log in page and then back to the app when successfully authorized. 如果用户在加载第一页时未登录,则将其重定向到SSO登录页面,然后在成功授权后返回到应用程序。 This flow is not something I can change. 我无法改变这种流程。

The problem I have is that if the user loads one module, works in that module for a while and then leaves the page open but doesn't work with it more during the day. 我的问题是,如果用户加载一个模块,则在该模块中工作了一段时间,然后使页面处于打开状态,但白天却无法再使用它。 If the user then the next day starts with trying to load a route that requires another lazy module, that module will return a 401 since he no longer is authenticated. 如果用户随后第二天开始尝试加载需要另一个惰性模块的路由,则该模块将返回401,因为他已不再通过身份验证。 However, the user will not see that the module failed to load and feels like nothing happens. 但是,用户将看不到模块加载失败,感觉没有任何反应。

I'm looking for a way to get notified when a lazy loaded module can't be loaded so I can redirect the user to the sign in page. 我正在寻找一种方法,以便在无法加载延迟加载的模块时得到通知,以便可以将用户重定向到登录页面。 Is this possible? 这可能吗?

By using a PreloadingStrategy that preloads the lazyloaded modules this can be prevented since the application no longer needs to GET the specific module on route change when the user is no longer authenticated. 通过使用PreloadingStrategy加载延迟加载模块的PreloadingStrategy,可以防止这种情况,因为当用户不再通过身份验证时,应用程序不再需要在路由更改时获取特定模块。

preload-strategy.ts (taken from John Papa repo link ) preload-strategy.ts(摘自John Papa repo链接

import { PreloadingStrategy, Route } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';

export class PreloadSelectedModulesList implements PreloadingStrategy {
  preload(route: Route, load: Function): Observable<any> {
    return route.data && route.data['preload'] ? load() : of(null);
  }
}

app-routing.module.ts APP-routing.module.ts

import { PreloadSelectedModulesList } from './core/preload-strategy';
    const routes: Routes = [
        // Eagerly loaded
        { path: '', component: HomeComponent, pathMatch: 'full' },
        // Preloaded module
        { path: 'preloaded', loadChildren: './preloaded/preloaded.module#PreloadModule', data: {preload: true}},
        // Lazy loaded module
        { path: 'lazy', loadChildren: './lazy/lazy.module#LazyModule'}
    ];

@NgModule({
    imports: [
        RouterModule.forRoot(routes, {
            preloadingStrategy: PreloadSelectedModulesList
        })
    ],
    exports: [RouterModule],
    providers: [ PreloadSelectedModulesList, ..., ... ]
})

export class AppRoutingModule { }

There could still be a small edge-case where the user suddenly becomes unauthorized while trying to GET a module, in this case one workaround could be what Abris mentions above, extending Angulars ErrorHandler and checking thrown errors for module loading. 仍然存在一个很小的极端情况,即用户在尝试获取模块时突然变得未经授权,在这种情况下,可能是Abris上面提到的一种解决方法,扩展了Angulars ErrorHandler并检查抛出的错误以加载模块。

error-handler.ts 错误handler.ts

import { ErrorHandler } from '@angular/core';

export default class MyErrorHandler extends ErrorHandler {

 constructor() { 
   super();
 }

  handleError(error) {
      // Check and take appropriate actions
      ...
      // delegate to the default handler
      super.handleError(error); 

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM