简体   繁体   English

api响应后如何延迟加载模块?

[英]How to lazy load module after api response in angular?

I want to lazy load a module after API response我想在 API 响应后延迟加载模块

below are my routes以下是我的路线

const routes: Routes = [
  { path: "", component: LandingComponent },
  { path: 'student', loadChildren: () => import('./student.module').then(m => m.StudentModule) },
  { path: 'school', loadChildren: () => import('./school.module').then(m => m.SchoolModule) },
  { path: "**", component: PagenotfoundComponent }
];

What I want is something like Facebook doing for profile handling for example例如,我想要的是 Facebook 为个人资料处理所做的事情

facebook.com/ABCD is my profile (A Different Module) facebook.com/ABCD 是我的个人资料(一个不同的模块)

facebook.com/EFGH is my page (Another Different Module) facebook.com/EFGH 是我的页面(另一个不同的模块)

In angular, I want something like when I access a URL like site.com/abcd it will first check if route param abcd is a user profile or an institution page and then based on the response it will lazy load the student or school module在 angular 中,我想要一些类似的东西,当我访问像site.com/abcd这样的 URL 时,它会首先检查路由参数abcd是用户个人资料还是机构页面,然后根据响应它会延迟加载学生或学校模块

I have a couple of ideas for this like make a common component and then in that component make API calls and check if route param is student profile or school profile then load their components accordingly something like below我对此有一些想法,例如制作一个通用组件,然后在该组件中进行 API 调用并检查路由参数是学生资料还是学校资料,然后相应地加载它们的组件,如下所示

const routes: Routes = [
  { path: "", component: LandingComponent },
  { path: ':profile_token', component : CommonComponent },
  { path: "**", component: PagenotfoundComponent }
];

then in CommonComponent然后在 CommonComponent 中

<ng-container *ngIf="isProfileRoute">
         <app-user-profile></app-user-profile>
</ng-container>

<ng-container *ngIf="isSchoolRoute">
         <app-school-profile></app-school-profile>
</ng-container>

But here in the method as you can see both components are loading and I feel its not a good way to handle it so if is there any way to lazy load a whole module containing routes and components based on API call it will be really great and helpful.但是在方法中,您可以看到两个组件都在加载,我觉得这不是处理它的好方法,因此如果有任何方法可以延迟加载包含基于 API 调用的路由和组件的整个模块,那将非常棒,并且有帮助。

Maybe dynamic loading in the routing could help you?也许路由中的动态加载可以帮助你?

app-routing.module.ts: app-routing.module.ts:

           {
                path: ':profile_token',
                loadChildren: async () => {
                    const service = AppInjector.get(YourService);
                    if (service.isStudent()) {
                        const a = await import('./modules/student/student.module');
                        return a['StudentModule'];
                    }
                    const b = await import('./modules/school/school.module');
                    return b['SchoolModule'];
                }
            }

And in the AppRoutingModule:在 AppRoutingModule 中:

export class AppRoutingModule {
    constructor(private injector: Injector) {
        AppInjector = this.injector;
    }
}

And before the routes array:在 routes 数组之前:

export let AppInjector: Injector;

The problem is that you are calling app-user-profile and app-school-profile in your common component.问题是您在公共组件中调用 app-user-profile 和 app-school-profile 。 If you want Angular to lazy load your module as soon as the route is called you have to use the Angular Router ( https://angular.io/guide/router ).如果您希望 Angular 在路由被调用后立即延迟加载您的模块,您必须使用 Angular Router ( https://angular.io/guide/router )。

You have to specify a where the router should create the content.您必须指定路由器应在何处创建内容。 In your common component you have to create a router redirect to either the profile or the school route:在您的公共组件中,您必须创建一个重定向到配置文件或学校路线的路由器:

this.router.navigate(['either the url of profile or the url of school']);

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

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