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-将数据加载到主数据库中,并更改要到达内存端点的组件中的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? "
谢谢菲利普!
这篇关于angular2 in-memory-web-api仅适用于应用程序的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!