【问题标题】:angular2-jwt: No provider for AuthConfigangular2-jwt:没有 AuthConfig 提供者
【发布时间】:2016-07-06 09:58:58
【问题描述】:

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

}

【问题讨论】:

    标签: angular ionic2 auth0


    【解决方案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
      ]
    })
    

    【讨论】:

    • 还是一样,no provider for AuthConfig
    【解决方案2】:

    奇怪的是你为AuthConfig定义了两次provider:

    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]
      })
    ]
    

    【讨论】:

      猜你喜欢
      • 2017-09-25
      • 2017-08-15
      • 2016-12-18
      • 2016-04-06
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 2016-12-20
      相关资源
      最近更新 更多