如何在main.ts中手动实例化Http服务 [英] How to instantiate Http service in main.ts manually
本文介绍了如何在main.ts中手动实例化Http服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在main.ts中手动实例化Http.我使用如下所示的HTTP_PROVIDERS找到了一些答案,但看起来似乎已弃用HTTP_PROVIDERS.知道我该怎么做吗?
I need to instantiate Http in main.ts manually. I found some answer using HTTP_PROVIDERS like below, but looks like HTTP_PROVIDERS is deprecated. Any idea how can I do this?
const injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);
const http = injector.get(Http);
然后我将使用此http对象来调用服务器,并在响应中引导appmodule.类似于以下代码:
I will then use this http object to call the server and bootstrap the appmodule in the response. Something like the code below:
http.get("app/config/config.json").map(res => res.json()).subscribe(resp=>{
platformBrowserDynamic().bootstrapModule(AppModule);
})
推荐答案
您可以使用Fetch API:
What you can do is to use the Fetch API:
fetch('config.json').then(res => res.json()).then((resp) =>{
platformBrowserDynamic().bootstrapModule(AppModule);
})
这篇关于如何在main.ts中手动实例化Http服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文