简体   繁体   English

Angular 4-使用HttpModule时出现错误“没有为ConnectionBackend提供程序!”

[英]Angular 4 - using HttpModule I get the error “No provider for ConnectionBackend!”

I'm new to Angular4 (and TypeScript) and I've been getting this error: 我是Angular4(和TypeScript)的新手,并且遇到了以下错误:

My app.module.ts is setup like this: 我的app.module.ts设置如下:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent }  from './app.component';
import { CustomerListComponent }  from './Customer-List/customer-list.component';
import { CustomerDetailsComponent }  from './Customer-Details/customer-details.component';
import { DataService } from './services/data.service';


@NgModule({
  imports:      [ BrowserModule, FormsModule, HttpModule ],
  declarations: [ AppComponent, CustomerListComponent, CustomerDetailsComponent ],
  providers: [ DataService ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

I have a service (called DataService) that uses the Http module in the following code: 我有一个在以下代码中使用Http模块的服务(称为DataService):

    import { Injectable } from '@angular/core';
    import { Http, Response, RequestOptions, Headers } from '@angular/http';
    import { CreateTestCustomers } from '../data/test-data';
    import { Customer } from '../Models/customerModel';

    @Injectable()
    export class DataService {

        private customersUrl = 'http://localhost:52740/api/Customers/';

        constructor(
            private http : Http
        ) { }

        getCustomersP() : Promise<Customer[]>{
                const customers = CreateTestCustomers();

                return this.http.get(this.customersUrl)
                    .toPromise()
                    .then(response => {
                        const custs = response.json().data as Customer[];
                        return custs;
                    }, error => {
                        return Promise.reject('Something bad happened, please check the console');
                    },);
            }
    }

When I try to compile my app, I get this strange error: 当我尝试编译我的应用程序时,出现以下奇怪错误:

Unhandled Promise rejection: No provider for ConnectionBackend! ; Zone: <root> ; Task: Promise.then ; Value: Error: No provider for ConnectionBackend! Error: No provider for ConnectionBackend!
   at ReflectiveInjector_.prototype._throwOrNull (eval code:2681:13)
   at ReflectiveInjector_.prototype._getByKeyDefault (eval code:2720:13)
   at ReflectiveInjector_.prototype._getByKey (eval code:2652:13)
   at ReflectiveInjector_.prototype.get (eval code:2521:9)
   at resolveNgModuleDep (eval code:9513:5)
   at _createClass (eval code:9555:13)
   at _createProviderInstance$1 (eval code:9524:13)
   at resolveNgModuleDep (eval code:9508:13)
   at _createClass (eval code:9555:13)
   at _createProviderInstance$1 (eval code:9524:13)
zone.js (661,17)
   "Unhandled Promise rejection:"
   "No provider for ConnectionBackend!"
   "; Zone:"
   "<root>"
   "; Task:"
   "Promise.then"
   "; Value:"
   {
      [functions]: ,
      __proto__: { },
      __zone_symbol__currentTask: { },
      description: "No provider for ConnectionBackend!",
      injectors: [ ],
      keys: [ ],
      message: "No provider for ConnectionBackend!",
      name: "Error",
      ngDebugContext: { },
      ngOriginalError: undefined,
      stack: "Error: No provider for ConnectionBackend!
   at ReflectiveInjector_.prototype._throwOrNull (eval code:2681:13)
   at ReflectiveInjector_.prototype._getByKeyDefault (eval code:2720:13)
   at ReflectiveInjector_.prototype._getByKey (eval code:2652:13)
   at ReflectiveInjector_.prototype.get (eval code:2521:9)
   at resolveNgModuleDep (eval code:9513:5)
   at _createClass (eval code:9555:13)
   at _createProviderInstance$1 (eval code:9524:13)
   at resolveNgModuleDep (eval code:9508:13)
   at _createClass (eval code:9555:13)
   at _createProviderInstance$1 (eval code:9524:13)"
   }
   "Error: No provider for ConnectionBackend!
   at ReflectiveInjector_.prototype._throwOrNull (eval code:2681:13)
   at ReflectiveInjector_.prototype._getByKeyDefault (eval code:2720:13)
   at ReflectiveInjector_.prototype._getByKey (eval code:2652:13)
   at ReflectiveInjector_.prototype.get (eval code:2521:9)
   at resolveNgModuleDep (eval code:9513:5)
   at _createClass (eval code:9555:13)
   at _createProviderInstance$1 (eval code:9524:13)
   at resolveNgModuleDep (eval code:9508:13)
   at _createClass (eval code:9555:13)
   at _createProviderInstance$1 (eval code:9524:13)"

Has anyone come across this before? 有人遇到过吗? Everything online seems to refer to Angular 2, and none of the fixes for it seem to help with angular 4. 在线上的所有内容似乎都涉及Angular 2,而对其进行的所有修复都似乎对Angular 4没有帮助。

I've seen this article but its unanswered - my problem looks similar: angular 4 Error: Uncaught (in promise): Error: No provider for ConnectionBackend! 我看过这篇文章,但未得到解答-我的问题看起来很相似: angular 4错误:未捕获(承诺):错误:没有ConnectionBackend的提供程序! while injecting Jsonp 在注入Jsonp时

Thanks in advance! 提前致谢!

Not so much as an answer, but a workaround. 与其说是答案,不如说是一种解决方法。 I scaffolded up a new project and added my components back in one by one to work around this issue. 我搭建了一个新项目,并逐个添加了我的组件以解决此问题。 Not sure what happened with the original project. 不知道原始项目发生了什么。

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

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