angular - 如何在main.ts中手动实例化Http服务

标签 angular http

我需要在 main.ts 中手动实例化 Http。我使用 HTTP_PROVIDERS 找到了一些答案,如下所示,但看起来 HTTP_PROVIDERS 已被弃用。知道我该怎么做吗?

const injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);
const http = injector.get(Http);

然后我将使用此 http 对象调用服务器并在响应中引导应用程序模块。类似于下面的代码:

http.get("app/config/config.json").map(res => res.json()).subscribe(resp=>{
      platformBrowserDynamic().bootstrapModule(AppModule);

})

最佳答案

您可以使用 Fetch API:

fetch('config.json').then(res => res.json()).then((resp) =>{
   platformBrowserDynamic().bootstrapModule(AppModule);
})

关于angular - 如何在main.ts中手动实例化Http服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46681396/

相关文章:

node.js - 带有 Angular2 项目的 ExpressJs 的文件结构

angular - 远程 Angular 应用程序和 Electron 桌面应用程序之间的通信

angular - 在 asp.net 5 Razor View 中使用 angular2

javascript - NODE.JS 和 EXPRESS 中的 URL 重定向处理程序

c# - 如何增加 .NET Core 中的传出 HTTP 请求配额?

http - http请求和将http请求文本写入80端口上的tcp/ip套接字有什么区别

java - HTTP 客户端 PUT 请求错误

angular - 创建 Angular/React 应用程序时的 .Net 本地主机服务器

angular - 如何在 Angular2 中渲染 dom 后调用函数?

python - 无法在 https 端口 (443) [Errno 10013] 上实例化 python BaseHTTPServer