无法解析PlatformService的所有参数:([object Object],?,[object Object])

时间:2017-07-07 10:55:05

标签: angular typescript dependency-injection

我有两项服务:PlatformServiceAccountService

import {Http} from '@angular/http';
import {AccountService} from "./account.service";
import {ToastService} from "./toast.service";
@Injectable()
export class PlatformService {
  constructor(
    private http: Http,
    private accountSvc: AccountService,
    private toastSvc: ToastService
  ) {}
  //...
}


import {Http} from "@angular/http";
import {ToastService} from "./toast.service";
@Injectable()
export class AccountService {
  constructor(
    private http: Http,
    private toastService: ToastService
  ){}
  //...
}

这是providers中的app.module.ts

providers: [
  StatusBar,
  SplashScreen,
  ToastService,
  AccountService,
  PlatformService,
  SocketService,
  ModelService,
  {provide: ErrorHandler, useClass: IonicErrorHandler}
]

我想将accountService注入我的platformService,但我收到以下错误:

  

无法解析PlatformService的所有参数:([object Object],?,[object Object])。

我认为我的代码中没有循环依赖,也许是因为platformServiceaccountService都注入httptoastService

我在private accountSvc: AccountService,的构造函数中评论PlatformService后,此错误消失了。

1 个答案:

答案 0 :(得分:1)

我终于找到了错误。我在RoomPreparePage中导入了AccountService, 我还在AccountService的构造函数中添加了RoomPreparePage,所以这似乎是一个循环依赖。 (但是,我没有在RoomPreparePage的构造函数中添加AccountService,我只是在AccountService的一种方法中使用它。)

删除RoomPreparePageAccountService的使用后,此问题已修复。