没有AuthHttp的提供者!在angular2-jwt上 [英] No provider for AuthHttp! on angular2-jwt

查看:150
本文介绍了没有AuthHttp的提供者!在angular2-jwt上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular 2上真的很新,我想在我的项目中使用JWT.因此,我完全按照 angular2-jwt官方页面中给出的指示进行操作>使用基本配置. 我使用以下代码创建一个名为auth.module.ts的文件:

im really new on Angular 2 and i want to use JWT on my project. So i follow the instructions exactly where a give from the official page of angular2-jwt using the basic configuration. I create a file named auth.module.ts with the following code:

import { NgModule } from '@angular/core';
import { Http, RequestOptions } from '@angular/http';
import { AuthHttp, AuthConfig } from 'angular2-jwt';

function authHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig(), http, options);
}

@NgModule({
  providers: [
    {
      provide: AuthHttp,
      useFactory: authHttpServiceFactory,
      deps: [Http, RequestOptions]
    }
  ]
})
export class AuthModule {}

下一步是发送经过身份验证的请求.我使用一个放置按钮的组件,该按钮调用一个函数,该函数执行页面上建议的代码:

The next step are sending a Authenticated Request. I use a component where i put a button that invokes a function that execute the code suggested on the page:

文件:calendario.components.ts

File: calendario.components.ts

import {Component, OnInit,trigger,state,style,transition,animate,keyframes, group} from '@angular/core';
import initDemo = require('../../../assets/js/charts.js');
import initNotify = require('../../../assets/js/notify.js');
import { AuthHttp } from 'angular2-jwt';

declare var $:any;

@Component({
    moduleId: module.id,
    selector: 'calendario',
    templateUrl: 'calendario.component.html'

})

export class CalendarioComponent{
    thing: any;
    constructor(public authHttp: AuthHttp) {}
    obtenerDatos() {
        this.authHttp.get('http://localhost/autorepuestos/web/app_dev.php/api/conjunto')
      .subscribe(
        data => this.thing = data,
        err => console.log(err),
        () => console.log('Request Complete')
      );
        console.log("Hola");
    }
}

当我在此组件上输入时,我出现错误:

When i enter on this component i have a error:

EXCEPTION: Uncaught (in promise): Error: No provider for AuthHttp!
Error: DI Error

我能解决这个问题的任何想法吗?我对angular 2真的很陌生,谢谢您的帮助!

Any idea that i can resolve this? I really new on angular 2, thanks for your help!

推荐答案

好像您已经创建了一个提供此AuthHttp类的模块.您是否已确定AuthModule位于CalendarioComponent所属模块的导入中?

Looks like you've created a module that will provide this AuthHttp class. Have you made sure that the AuthModule is in the imports of the module that CalendarioComponent is a part of?

类似这样的东西:

import { AuthModule } from '...';
import { CalendarioComponent } from '...';

@NgModule({
    imports: [AuthModule],
    declarations: [CalendarioComponent]
})
export class SomeModule {}

这篇关于没有AuthHttp的提供者!在angular2-jwt上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆