简体   繁体   English

Angular 2服务错误-没有Http的提供程序

[英]Angular 2 Service Error - No provider for Http

I'm currently receiving the following error: 我目前收到以下错误:

EXCEPTION: No provider for Http! (Login -> AuthHttp -> Http)

AuthHttp is a reference to Angular2-jwt. AuthHttp是对Angular2-jwt的引用。 Could this be an issue with that? 这可能是一个问题吗? I've been staring at this for a while, and I can't find what error I could possibly have. 我凝视了一段时间了,我找不到我可能遇到的错误。

In my login.ts: 在我的login.ts中:

import {Component} from 'angular2/core';
import {
  FORM_DIRECTIVES,
  FormBuilder,
  ControlGroup,
  Validators,
  AbstractControl,
  Control
} from 'angular2/common';
import {Http, Headers} from 'angular2/http';
import {Router} from 'angular2/router';

import {ButtonRadio} from 'ng2-bootstrap/ng2-bootstrap';
import {AuthHttp, AuthConfig} from 'angular2-jwt';

import {AuthService} from '../../services/auth/authService';
import {AlertService} from '../../services/alerts/alertsService';
import {User} from '../../datatypes/user/user';

@Component({
  selector: 'login',
  template: require('./login.html'),
  directives: [ ButtonRadio, FORM_DIRECTIVES ],
  providers: [AuthService, AlertService, Http, AuthHttp]
})

In my main.ts: 在我的main.ts中:

document.addEventListener('DOMContentLoaded', function main() {
  bootstrap(App, [
    ('production' === process.env.ENV ? [] : ELEMENT_PROBE_PROVIDERS),
    HTTP_PROVIDERS,
    ROUTER_PROVIDERS,
    provide(LocationStrategy, { useClass: HashLocationStrategy }),
    provide(AuthConfig, { useFactory: () => {
      return new AuthConfig({
        headerName: 'Authorization',
        //headerPrefix: 'Bearer ',
        tokenName: 'auth_token',
        noJwtError: true
      });
    }}),
    AuthHttp,
    Http,
    ConnectionBackend
  ])
  .catch(err => console.error(err));
});

You have to add HTTP_BINDINGS 您必须添加HTTP_BINDINGS

import {HTTP_BINDINGS} from 'angular2/http';


bootstrap(..., [
  ...
  HTTP_BINDINGS,
  ...
]).catch(...);

It seems that this was indeed an error with Angular2-jwt, or at least with its configuration. 看来这确实与Angular2-jwt或至少与其配置有关的错误。 Here is the way it should be configured. 这是应该配置的方式。

Still, I've managed to clean things up a bit. 尽管如此,我还是设法清理了一下。 If this problem actually runs deeper, I'll try to reopen. 如果此问题实际上更严重了,我将尝试重新打开。

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

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