Angular forRoot方法中的角度调用函数

Angular forRoot方法中的角度调用函数,angular,angular-cli,angular-module,angular-library,Angular,Angular Cli,Angular Module,Angular Library,问题是我在forRoot方法中调用的函数如下: { "production": "false", "SENTRY_URL": "https://...@sentry.com/whatever/1" } export const OPTIONS = new InjectionToken<string>('OPTIONS'); export interface MyModuleOptions { config: { sentryURLGetter: () =>

问题是我在forRoot方法中调用的函数如下:

{
  "production": "false",
  "SENTRY_URL": "https://...@sentry.com/whatever/1"
}
export const OPTIONS = new InjectionToken<string>('OPTIONS');

export interface MyModuleOptions {
  config: {
    sentryURLGetter: () => string | Promise<string>;
  }
}

export function initialize(options: any) {
  console.log('sentryURL', options.config.sentryURLGetter());
  return function () {
  };
}

@NgModule({
  imports: [
    CommonModule
  ]
})
export class MyModule {
  static forRoot(options: MyModuleOptions): ModuleWithProviders {
    return {
      ngModule: MyModule,
      providers: [
        {provide: OPTIONS, useValue: options},
        {
          provide: APP_INITIALIZER,
          useFactory: initialize,
          deps: [OPTIONS],
          multi: true
        }
      ]
    };
  }
}
应用程序模块.ts

import {environment} from '../environments/environment';

...

@NgModule({
  imports: [
    BrowserModule,
    MyModule.forRoot({
      config: {
        sentryURL: environment.SENTRY_URL <-- This, calls the function
      }
    }),
    HttpClientModule,
    ...
 ]})
export function loadJSON(filePath) {
  const json = loadTextFileAjaxSync(filePath, 'application/json');
  return JSON.parse(json);
}

export function loadTextFileAjaxSync(filePath, mimeType) {
  const xmlhttp = new XMLHttpRequest();
  xmlhttp.open('GET', filePath, false);
  if (mimeType != null) {
    if (xmlhttp.overrideMimeType) {
      xmlhttp.overrideMimeType(mimeType);
    }
  }
  xmlhttp.send();
  if (xmlhttp.status === 200) {
    return xmlhttp.responseText;
  } else {
    return null;
  }
}

export const environment = loadJSON('/assets/config.json');
配置如下所示:

{
  "production": "false",
  "SENTRY_URL": "https://...@sentry.com/whatever/1"
}
export const OPTIONS = new InjectionToken<string>('OPTIONS');

export interface MyModuleOptions {
  config: {
    sentryURLGetter: () => string | Promise<string>;
  }
}

export function initialize(options: any) {
  console.log('sentryURL', options.config.sentryURLGetter());
  return function () {
  };
}

@NgModule({
  imports: [
    CommonModule
  ]
})
export class MyModule {
  static forRoot(options: MyModuleOptions): ModuleWithProviders {
    return {
      ngModule: MyModule,
      providers: [
        {provide: OPTIONS, useValue: options},
        {
          provide: APP_INITIALIZER,
          useFactory: initialize,
          deps: [OPTIONS],
          multi: true
        }
      ]
    };
  }
}
当我使用aot进行构建时,它会说:

src/app/app.module.ts(41,20)中出错:“AppModule”的模板编译期间出错 decorators中不支持函数调用,但在“环境”中调用了“loadJSON” “环境”调用“loadJSON”

有什么想法吗

:)

更新的解决方案:

我的最终解决方案是,在应用程序中,如Suren Srapyan所说,使用函数getter。在库中,forRoot方法应如下所示:

{
  "production": "false",
  "SENTRY_URL": "https://...@sentry.com/whatever/1"
}
export const OPTIONS = new InjectionToken<string>('OPTIONS');

export interface MyModuleOptions {
  config: {
    sentryURLGetter: () => string | Promise<string>;
  }
}

export function initialize(options: any) {
  console.log('sentryURL', options.config.sentryURLGetter());
  return function () {
  };
}

@NgModule({
  imports: [
    CommonModule
  ]
})
export class MyModule {
  static forRoot(options: MyModuleOptions): ModuleWithProviders {
    return {
      ngModule: MyModule,
      providers: [
        {provide: OPTIONS, useValue: options},
        {
          provide: APP_INITIALIZER,
          useFactory: initialize,
          deps: [OPTIONS],
          multi: true
        }
      ]
    };
  }
}
export const OPTIONS=new InjectionToken('OPTIONS');
导出接口MyModuleOptions{
配置:{
sentryURLGetter:()=>string | Promise;
}
}
导出函数初始化(选项:任意){
log('sentryURL',options.config.sentryURLGetter());
返回函数(){
};
}
@NGD模块({
进口:[
公共模块
]
})
导出类MyModule{
静态forRoot(选项:MyModuleOptions):ModuleWithProviders{
返回{
ngModule:MyModule,
供应商:[
{提供:选项,使用值:选项},
{
提供:应用程序初始化器,
useFactory:初始化,
副署长:[选择],
多:真的
}
]
};
}
}

:D

函数调用在
@Decorators
中不受支持。或者,您可以在
@NgModule
之外获取值,然后使用它的值

export function getSentryUrl() {
   return environment.SENTRY_URL;
}

@NgModule({
   imports: [
      BrowserModule,
      MyModule.forRoot({
      config: {
        getSentryURL: getSentryUrl
      }
    }),
    HttpClientModule,
    ...
 ]})

我已经更新了。你也可以这样试试。在
forRoot
中,称之为nice。forRoot()方法的参数是什么?在我的例子中是模块的配置。你能为plunker提供更新的解决方案吗?不是plunker而是hole项目,