2017-03-06 58 views
0

我有一个供应商,我想使用APP_INITIALIZER上的应用程序初始化运行它,但不知何故,我不断收到一个错误角2调用应用程序启动时服务

Unhandled Promise rejection: appInits[i] is not a function ; Zone: ; Task: Promise.then ; Value: TypeError: appInits[i] is not a function

这里是我的供应商:

@Injectable() 
export class SendNotification { 
    constructor(public http: Http) { 
    Observable.interval(30 * 60 * 1000) 
      .switchMap(res =>this.http.get(`http://localhost:8100/api/balance.pl?meternumber=0003080123&api=json`)) 
      .map(res => res.json()) 
      .subscribe(res => this.check(res)) 
} 

private check(res) { 
    console.log("Check wether to notify a user") 
} 

而且我app.module.ts,我打电话给供应商如下:

providers: [{provide: APP_INITIALIZER,useClass: SendNotification,deps:[Http],multi: true}] 
}) 

任何帮助将不胜感激?

+0

的方法'appInits'从你的代码名? –

+0

不,我没有在我的代码... – bobin56

回答

0

我认为你需要使用useFactory和调用返回一个PromiseObservable

function notificationInitializer() :() => Promise<any> { 
    return() => Promise.resolve(null); 
} 

providers: [ 
    SendNotification, 
    { provide: APP_INITIALIZER, useFactory: notificationInitializer, 
     deps:[Http, SendNotification], 
     multi: true}] 
}) 
+0

感谢您的帮助,一个快速的问题,notificationInitializer函数应该去哪里? – bobin56

+0

并不重要。无论是在同一文件中还是在导入到当前文件的文件中,以便引用它的提供程序都是有效的TS。 –

+0

仍然收到相同的错误 – bobin56

相关问题