简体   繁体   中英

Angular 11 - Call method in module forRoot

I'm actually facing a problem with Angular. Instead of having a static array of object in the "forRoot" methode, I want to pass an array sent from the back.

Does anyone know how to perform this action please?

Code:


import { NgModule } from '@angular/core';
import { AbTestOptions, AbTestsModule } from 'angular-ab-tests';

export const abTestsOptions: AbTestOptions[] = [
    {
        versions: ['test1', 'test2'],
        scope: 'scope1',
        expiration: 1,
        weights: { test1: 10 }
    },
];

@NgModule({
    imports: [
        AbTestsModule.forRoot(abTestsOptions),
    ],
})
export class AbTestingModule {
}

Angular module imports are resolved at compile time by the angular compiler, and therefore can not depend on knowledge only obtained at runtime.

What you can do is having different modules for the different environments, or inject a dynamic configuration object using a factory provider that is then used in conditionals, but you can not have the metadata used by the angular compiler depend on runtime values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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