在我的app.module.ts
中@NgModule({
...
providers: [MyService,
// providers used to create fake backend
fakeBackendProvider,
MockBackend,
BaseRequestOptions
],
bootstrap: [AppComponent]
})
fakeBackendProvider,MockBackend和BaseRequestOptions用于模拟后端。有没有办法通过environment.ts文件中的变量注入这些服务?
答案 0 :(得分:0)
你可以像这样使用它。创建了Plunker
//route to environment file
import { environment } from '../../environments/environment';
providers: [MyService,
// providers used to create fake backend
{
provide: SomeSerivce,
useFactory: authHeadersFactory
},
],
export function authHeadersFactory() {
if (environment.prod) {
return new Mock();
}
return new fakeBackend();
}