简体   繁体   English

将 HttpModule(已弃用)与 Angular 9

[英]Using HttpModule (deprecated) with Angular 9

I recently updated from Angular 4 to 9. I did the following:我最近从 Angular 4 更新到 9。我做了以下事情:

  1. Created new Angular 9 project, copied all code into angular 9 project创建新的 Angular 9 项目,将所有代码复制到 angular 9 项目中
  2. Updated rxjs (added pipes where necessary, removed concat)更新rxjs (在必要时添加了管道,删除了连接)
  3. Fixed easy to fix warnings (unused modules variables etc)修复了易于修复的警告(未使用的模块变量等)
  4. Installed HttpModule (deprecated) to eliminate errors relating to 'cannot find HTTP' errors and get my project back on track安装了HttpModule (已弃用)以消除与“找不到 HTTP”错误相关的错误,并使我的项目重回正轨

I am now at the point where running ng build / ng serve does not throw any errors, and I am able to visit local host and get my landing page.我现在处于运行ng build / ng serve不会引发任何错误的地步,并且我能够访问本地主机并获取我的登录页面。 However, using my old login form does not work.但是,使用我的旧登录表单不起作用。 I keep getting a CORS error, though I have the following headers set in express :我不断收到CORS错误,尽管我在express中设置了以下标头:

app.use(function(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
  next();
});

Relevant Package Details: "@angular/core": "~9.1.4" <-- all angular components are at this point, "@angular/common": "~9.1.4", "@angular/http": "^7.2.16", "express": "^4.17.1"

My questions are:我的问题是:

  1. Is it possible to run an angular 9 app with the HTTP module as I am trying to do?是否可以像我尝试的那样使用HTTP module运行 angular 9 应用程序? If anyone has had success with it, were there any configuration changes / was it plug and play?如果有人成功了,是否有任何配置更改/即插即用?
  2. Is it possible the CORS error could be related to using HTTP module rather than updating to httpClient? CORS 错误是否可能与使用HTTP module而不是更新到 httpClient 有关?
  3. If I have to update to httpClientModule , is there a tool/utility that can be used to make this update easier?如果我必须更新到httpClientModule ,是否有可用于简化此更新的工具/实用程序?

It is not so easy to upgrade 5 major versions at one shot.一口气升级 5 个主要版本并非易事。 You should better follow the updated guide您最好遵循更新的指南

As for the HttpModule , it is deprecated and removed after v7.至于HttpModule ,它在 v7 之后已被弃用并删除。 It is much easier to upgrade to new HttpClientModule than trying to make something removed working.升级到新的HttpClientModule比尝试使删除的内容正常工作要容易得多。

For people who are coming across this, here was how I ended up resolving my upgrade problems:对于遇到此问题的人,这是我最终解决升级问题的方法:

  1. I created an new project with angular CLI.我使用 angular CLI 创建了一个新项目。
  2. I moved all my code into the new project and then went through and fixed any exceptions one at a time.我将所有代码移到新项目中,然后一次通过并修复任何异常。

I wish there was an easier way to do it because this was a time consuming process.我希望有一种更简单的方法来做到这一点,因为这是一个耗时的过程。 However, in the end it worked out and I was able to upgrade the entire project.但是,最终它成功了,我能够升级整个项目。

I know this question has been downvoted - I'm not sure why, especially since it has had over 1,000 views.我知道这个问题被否决了——我不知道为什么,特别是因为它有超过 1,000 次观看。 Hopefully my answer helps anyone else struggling with this issue.希望我的回答能帮助其他在这个问题上苦苦挣扎的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM