angular2-jwt:没有AuthConfig的提供者

时间:2016-03-19 17:31:00

标签: angular ionic2 auth0

我想在我的angular2-jwt应用程序中使用ionic2并继续No provider for AuthConfig!

这是我的app.ts

import {AuthHttp, AuthConfig} from 'angular2-jwt';
import {Http} from 'angular2/http'
@App({
  template: '<ion-nav [root]="rootPage"></ion-nav>',
  config: {}, // http://ionicframework.com/docs/v2/api/config/Config/
  providers: [
     provide(AuthHttp, {
      useFactory: (http) => {
        return new AuthHttp(new AuthConfig(), http);
      },
      deps: [Http]
     }),
    AuthHttp
  ]
})

我正在login.ts页面上使用它:

import {AuthHttp, AuthConfig} from 'angular2-jwt';
@Page({
  templateUrl: 'build/pages/login/login.html',
  directives: [IONIC_DIRECTIVES]
})
export class LoginPage {
     constructor(private authHttp: AuthHttp){
     }

}

2 个答案:

答案 0 :(得分:5)

您为AuthConfig定义了两倍的提供程序,这很奇怪:

providers: [
  provide(AuthHttp, {
    useFactory: (http) => {
      return new AuthHttp(new AuthConfig(), http);
    },
    deps: [Http]
   }),
  AuthHttp // <-----------
]

第二个将覆盖第一个参数,并期望AuthConfig作为第一个参数注入AuthHttp。但是AuthConfig没有提供商。

应该删除第二个AuthHttp,如下所述:

providers: [
  provide(AuthHttp, {
    useFactory: (http) => {
      return new AuthHttp(new AuthConfig(), http);
    },
    deps: [Http]
  })
]

答案 1 :(得分:-1)

尝试添加AuthConfig作为依赖项:

import {AuthHttp, AuthConfig} from 'angular2-jwt';
import {Http} from 'angular2/http'
@App({
  template: '<ion-nav [root]="rootPage"></ion-nav>',
  config: {}, // http://ionicframework.com/docs/v2/api/config/Config/
  providers: [
     provide(AuthHttp, {
      useFactory: (http) => {
        return new AuthHttp(new AuthConfig(), http);
      },
      deps: [Http,AuthConfig]
     }),
    AuthHttp
  ]
})