我想在我的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){
}
}
答案 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
]
})