简体   繁体   English

在Angular 7中将工厂方法从AppModule移至AppComponent

[英]Move factory method from AppModule to AppComponent in Angular 7

I use ngx-translate-multi-http-loader loader to create specific translations. 我使用ngx-translate-multi-http-loader loader创建特定的翻译。 The documentation requires that this factory has to be created within the app.module.ts: 该文档要求必须在app.module.ts中创建该工厂:

export function multiTranslateHttpLoaderFactory(http: HttpClient) {
  return new MultiTranslateHttpLoader(http, [
    {prefix: './assets/i18n/default/', suffix: '.json'},
    {prefix: './assets/i18n/bc/', suffix: '.json'}
  ]);
}

This factory is configured inside the import arrays in this way: 通过以下方式在导入数组中配置此工厂:

TranslateModule.forRoot({
  loader: {
    provide: TranslateLoader,
    useFactory: multiTranslateHttpLoaderFactory,
    deps: [HttpClient]
  }
}),

I need a way to put the factory into my app.component.ts, because I still have to program some logic and do not want to do that within my app.module.ts. 我需要一种将工厂放入我的app.component.ts中的方法,因为我仍然必须编写一些逻辑,并且不想在我的app.module.ts中这样做。

But if I move the method into my app.component.ts and make all the necessary import statements on top of the file, angular doesn't compile and returns this error message: 但是,如果我将方法移到我的app.component.ts中,并在文件顶部制作了所有必要的import语句,则angular不会编译并返回以下错误消息:

ERROR in src / app / app.module.ts (55,21): error TS2304: Can not find name 'multiTranslateHttpLoaderFactory'. src / app / app.module.ts(55,21)中的错误:错误TS2304:找不到名称“ multiTranslateHttpLoaderFactory”。

Is there a possibility or do I really have to define the factory within app.module.ts ??? 有没有可能或者我真的必须在app.module.ts中定义工厂???

It really should not matter where you define your factory, or any other function for that matter. 定义工厂的位置或与此相关的任何其他功能都没关系。 Just be sure to import it correctly, somewhere in the top of app.module.ts 只要确保将其正确导入即可,位于app.module.ts顶部的app.module.ts

import {multiTranslateHttpLoaderFactory} from 'path/to/your/component'

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

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