我希望导入从服务文件返回的值,例如服务文件为:
new Enemy(player)
但是如何将这个值添加到我的app.module文件中。有没有首选的方法?
我尝试过:
http.service.ts
selectedEnvironment()
{
return "abc";
}
但是它说“类型HttpService的类型上不存在属性'selectedEnvironment'”。
答案 0 :(得分:1)
如果它是Angular服务
import { Injectable } from "@angular/core";
@Injectable()
export class YourHttpService {
constructor() {}
getEnvironment() {
return "XYZ";
}
}
您可以在 HttpLoaderFactory 中将服务指定为参数。请确保在 deps:[HttpClient,YourHttpService ]以及应用模块的提供商提供者:[YourHttpService] 中指定了服务名称
export function HttpLoaderFactory(httpClient: HttpClient,httpService:YourHttpService) {
console.log(httpService.getEnvironment());
return new TranslateHttpLoader(httpClient);
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient,YourHttpService]
}
})
],
providers: [YourHttpService],
bootstrap: [AppComponent]
})
export class AppModule { }
样本代码 https://stackblitz.com/edit/transalate-vvmckq?file=src/app/app.module.ts
答案 1 :(得分:0)
在app.module.ts中添加
providers: [HttpService]