0

我正在遵循Scaldi 文档中提到的步骤。以下是我的代码。

class Game(players: List[Player], currentPlayer: Player,
           board: Board, active: Boolean, gamePersistor: GamePersistor) extends Injectable {

  def this(players: List[Player], currentPlayer: Player,
           board: Board, active: Boolean)(implicit inj: Injector) {
    this(players, currentPlayer, board, active, inject[GamePersistor])
  }
}

我收到以下编译错误。

Error:(11, 49) not found: value inject
    this(players, currentPlayer, board, active, inject[GamePersistor])
                                                ^

有人可以帮我解决这个问题吗?

4

1 回答 1

1

从文档中弄清楚

所有形式的注入期望和 Injector 的隐式实例都在范围内。如果

您正在注入模块定义,那么它已经为您提供了一个。如果您在自己的类中注入,那么最好的方法是提供隐式注入器实例作为构造函数参数,如上面的示例所示。

所以代码应该是

class Game(players: List[Player], currentPlayer: Player,
           board: Board, active: Boolean, gamePersistor: GamePersistor)(implicit inj:Injector) extends Injectable {

  def this(players: List[Player], currentPlayer: Player,
           board: Board, active: Boolean)(implicit inj: Injector) {
    this(players, currentPlayer, board, active, inject[GamePersistor])
  }
}
于 2014-12-30T11:40:36.283 回答