angular2 in-memory-web-api 仅用于应用程序的一部分 [英] angular2 in-memory-web-api only for a part of an application
问题描述
我想知道是否可以配置 angular2 in-memory-web-api 仅适用于应用程序的一部分.我想访问已完成组件的外部端点,并为开发阶段的组件使用 in-memory-web-api.
I was wondering if is possible to configure angular2 in-memory-web-api only for a part of an application. I want to reach external endpoints for finished components and use in-memory-web-api for components in development stage.
我尝试通过以下两种方式做到这一点:
I've tried to do this in the folowing two ways:
1 - 在 main 中加载数据并在我想要到达内存端点的组件中更改 XHRBackend;
1 - Loading the data in the main and changing the XHRBackend in the components that I want to reach in-memory endpoints;
main.ts
bootstrap(AppComponent, [
...
{ provide: SEED_DATA, useClass: InMemoryDataService }
]);
inDevelopmentStage.service.ts
inDevelopmentStage.service.ts
@Component({
providers: [
{ provide: XHRBackend, useClass: InMemoryBackendService }
]
})
2 - 在我想要到达内存端点的组件中加载数据并更改 XHRBackend;
2 - Loading the data and changing the XHRBackend in the components that I want to reach in-memory endpoints;
inDevelopmentStage.service.ts
inDevelopmentStage.service.ts
@Component({
providers: [
{ provide: XHRBackend, useClass: InMemoryBackendService }, // in-mem server
{ provide: SEED_DATA, useClass: InMemoryDataService } // in-mem server data
]
})
有什么办法可以实现这个目标吗?
Is there any way that I can achieve this goal?
感谢您的帮助!
推荐答案
正如 filipesilva 这里,第二种方法应该可行.
As pointed by filipesilva here, the second way should work.
"哦,当然,你绝对可以做到这一点.每个组件都有自己的注入器,它为自己和该子树中的所有组件解析提供程序.
" Oh sure, you can definitely do this. Each component has it's own injector, which is what resolves providers for itself and all components in that subtree.
你的第二个例子应该有效:
Your second example should have worked:
@Component({
providers: [
{ provide: XHRBackend, useClass: InMemoryBackendService }, // in-mem server
{ provide: SEED_DATA, useClass: InMemoryDataService } // in-mem server data
]
})
对于那个组件及其子树,XHRBackend 是 InMemoryBackendService 而不是 HTTP_PROVIDERS 提供的服务.
As for that component and it's subtree, XHRBackend is InMemoryBackendService instead of the service provided by HTTP_PROVIDERS.
请记住,它也适用于该子树中的任何其他组件.也许这就是问题所在?"
Remember that it also applies to any other components in that subtree however. Maybe that's what was going wrong? "
谢谢 Filipe!
这篇关于angular2 in-memory-web-api 仅用于应用程序的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!