Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';在使用--aot标志运行时,在Angular模块中为根导入加载动态参数_Angular_Angular Cli_Angular Module_Angular Aot_Nrwl - Fatal编程技术网

Can';在使用--aot标志运行时,在Angular模块中为根导入加载动态参数

Can';在使用--aot标志运行时,在Angular模块中为根导入加载动态参数,angular,angular-cli,angular-module,angular-aot,nrwl,Angular,Angular Cli,Angular Module,Angular Aot,Nrwl,我试图在--aot标志期间编译代码,但在我尝试将环境配置加载到我的日志记录服务(我在app.module.ts文件中导入)时失败。 我有这样的代码,与“ng serve”一起运行良好 这就是我想要动态传递给我的单例日志服务的结构 // In it's own ts file, not in the module export interface AppConfiguration { user: User; application: ApplicationConfig; } //

我试图在--aot标志期间编译代码,但在我尝试将环境配置加载到我的日志记录服务(我在app.module.ts文件中导入)时失败。 我有这样的代码,与“ng serve”一起运行良好

这就是我想要动态传递给我的单例日志服务的结构

// In it's own ts file, not in the module
export interface AppConfiguration {   
  user: User;
  application: ApplicationConfig;
}

// In it's own ts file, not in the module
export const appConfig = (): AppConfiguration => {
  // ... logic happens here to set up the environment, load cookies etc that will result something like this
  const userConf: User = {username: 'ronnrak', name: 'Ronny Raketgevär', id: 1, location: 'a place'};
  const appConf: ApplicationConfig = {defaultLogLevel: 'debug', defaultLogType: 'console'};
  return {user: userConf, application: appConf};
}
logging.module.ts:

export const APP_CONFIG = new InjectionToken<AppConfiguration>('app_config');

@NgModule({
  imports: [CommonModule]
})
export class LoggingModule {
  constructor(@Optional() @SkipSelf() parentModule: LoggingModule) {
    if(parentModule) {
      throw new Error('LoggingModule is already loaded, import in AppModule only.');
    }
  }

  static forRoot(config: AppConfiguration): ModuleWithProviders {
    return {
      ngModule: LoggingModule,
      providers: [
        {provide: APP_CONFIG, useValue: config},
        {provide: LoggingService, useFactory: (conf: AppConfiguration) => {
          return conf.Application.defaultLogType === 'console' ? new ConsoleLoggingService(conf) : new RestLoggingService(conf);
        }, deps:[APP_CONFIG]}
      ]
    };
  }
}
我试图在没有lambda表达式和一个明确导出的函数的情况下更改appConfig,但它也不起作用

ERROR in apps\project\src\app\app.module.ts(17,49): Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'appConfig' was called.

如何将动态数据从app.module加载到singleton服务中?

您应该导出LoggingService和StoreService的
useFactory
功能,因为,我引用了trichetriche

您不能在装饰器中使用函数调用

伐木服务

export function loggingFactory(conf: AppConfiguration) {
  return conf.Application.defaultLogType === 'console' ? 
    new ConsoleLoggingService(conf) : new RestLoggingService(conf);
}

{
  provide: LoggingService, 
  useFactory: loggingFactory, 
  deps:[APP_CONFIG]
}
以及商店服务:

export function storeFactory(conf: AppConfiguration) {
  return new StoreService(conf);
}

{
  provide: StoreService, 
  useFactory: storeFactory, 
  deps: [APP_CONFIG]
},
但是这个似乎有点过时,因为您可以直接使用该服务并使用
@inject()


您应该导出LoggingService和StoreService的
useFactory
功能,因为,我引用trichetriche

您不能在装饰器中使用函数调用

伐木服务

export function loggingFactory(conf: AppConfiguration) {
  return conf.Application.defaultLogType === 'console' ? 
    new ConsoleLoggingService(conf) : new RestLoggingService(conf);
}

{
  provide: LoggingService, 
  useFactory: loggingFactory, 
  deps:[APP_CONFIG]
}
以及商店服务:

export function storeFactory(conf: AppConfiguration) {
  return new StoreService(conf);
}

{
  provide: StoreService, 
  useFactory: storeFactory, 
  deps: [APP_CONFIG]
},
但是这个似乎有点过时,因为您可以直接使用该服务并使用
@inject()


这是一个众所周知的问题,Angular的存储库中打开了几个。“您根本不能使用函数调用来修饰程序。”特里谢说得对。这是一个众所周知的问题,Angular的存储库中打开了几个问题。“您根本不能使用函数调用来修饰程序。”特里谢说得对。这是一个很好的问题/修复示例,我应该将useFactory函数导出到哪里,以及如何调用它们?@Sam您可以将它们放在
@NgModule
声明上方,而您不必调用它们,您可以将它们用作
useFactory
值,angular将为您调用它们。它起作用了!我必须删除我的LoggingModule导入,并使用您的代码示例将其添加到提供程序中。非常感谢。我应该将useFactory函数导出到哪里,以及如何调用它们?@Sam您可以将它们放在
@NgModule
声明上方,而您不必调用它们,您可以将它们作为
useFactory
值使用,angular将为您调用它们。它成功了!我必须删除我的LoggingModule导入,并使用您的代码示例将其添加到提供程序中。非常感谢。
@Injectable()
export class StoreService {
  constructor(@Inject(APP_CONFIG) appConfig: AppConfiguration) {}
}