3

我想在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){
     }

}
4

2 回答 2

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]
  })
]
于 2016-03-19T19:31:53.280 回答
-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
  ]
})
于 2016-03-19T17:46:37.680 回答