我尝试使用angular2 http请求,但出现以下错误:
[错误]异常(exception):没有ConnectionBackend的提供者! (AppComponent-> Http-> ConnectionBackend)

我已经安装了具有相同问题的准系统应用程序,并且对造成它的原因不知所措。

提前致谢

app.component.ts

import {Component} from 'angular2/core';
import {Http, Headers} from 'angular2/http';

@Component({
  selector: 'app',
  template: `
    <button (click)="getEmployee()">Get</button>
    <p>{{employee.email}}</p>

  `,
  providers: [Http]
})

export class AppComponent {
  employee: {"email": "bob"};
  http: Http;

  constructor(http:Http){
    this.http = http;
  }

  getEmployee(){
    this.http.get('localhost:8080/employee-rates?id=0')
      .map(response => response.json()).subscribe(result => this.employee = result);
  }


}

主要
import {AppComponent} from './app.component';
import {bootstrap} from 'angular2/platform/browser';
import {HTTP_PROVIDERS, HTTP_BINDINGS} from 'angular2/http';

bootstrap(AppComponent, [
  HTTP_PROVIDERS, HTTP_BINDINGS
]);

index.html
<html>

  <head>
    <base href="/">
    <title>Test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="js/es6-shim/es6-shim.min.js"></script>
    <script src="js/systemjs/dist/system-polyfills.js"></script>

    <script src="js/angular2/bundles/angular2-polyfills.js"></script>
    <script src="js/systemjs/dist/system.src.js"></script>
    <script src="js/rxjs/bundles/Rx.js"></script>
    <script src="js/angular2/bundles/angular2.dev.js"></script>
    <script src="js/angular2/bundles/http.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
      System.config({
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>

  </head>

  <!-- 3. Display the application -->
  <body>
    <div class='small-12'>
      <app>Loading...</app>
    </div>
  </body>

</html>

最佳答案

从您的代码中删除HTTP_BINDINGS。它们已弃用,并引用了HTTP_PROVIDERS,因此就像您在两次使用提供程序一样。

bootstrap(AppComponent, [HTTP_PROVIDERS]);

另外,由于您在 bootstrap 中注入(inject)了providers: [Http],因此您的组件中也不需要HTTP_PROVIDERS。将其添加到组件的提供程序中将创建服务的新实例。
@Component({
  providers: []
})

关于http - Angular2/http异常,无ConnectionBackend,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35586874/

10-10 10:35