声明模块类后,我的ast
文件中有一些功能
app.module.ts
并在导入部分中使用它们
export function exportTranslateStaticLoader(http: HttpClient, config: RuntimeConfigLoaderService, transferState: TransferState, cookie: CookieService) {
let apiUrl;
if (environment.browser.symphonyApiUrl) {
apiUrl = environment.browser.symphonyApiUrl;
} else {
const protocol = window.location.protocol;
const host = window.location.host;
apiUrl = `${protocol}//api.${host}`;
}
const link = environment.remoteTranslate ? `${apiUrl}/cms/locale/` : '/assets/i18n/';
return new TranslateBrowserLoader(link, '.json', transferState, http, cookie);
}
我需要在此功能中使用某些服务(TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: exportTranslateStaticLoader,
deps: [HttpClient, TransferState, CookieService]
}
}),
),如何使用?
答案 0 :(得分:0)
您可以使用中间服务@Injectable
来确保HttpClient
和其他服务作为单例提供,
那么TranslateBrowserLoader
应该是此@Injectable
中函数的返回类型。
@Injectable()
export class TranslateLoader {
constructor(
private http: HttpClient,
private config: RuntimeConfigLoaderService,
private transferState: TransferState,
private cookie: CookieService
) {}
// import exportTranslateStaticLoader
getTranslateStaticLoader() : TranslateBrowserLoader {
return exportTranslateStaticLoader(
this.http,
this.config,
this.transferState,
this.cookie
)
}