当我使用ViewModel.Factory时,出现`Kodein No binding found for bind<Kodein>()`错误
When I use ViewModel.Factory, appear `Kodein No binding found for bind<Kodein>()` error
我参考http://kodein.org/Kodein-DI/?6.1/android#view-model-factory使用kodein viewmodel,出现错误。
零件编码
bind<ViewModelProvider.Factory>() with singleton { KodeinViewModelFactory(instance()) }
class KodeinViewModelFactory(private val kodein: Kodein) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T =
kodein.direct.Instance(TT(modelClass))
}
很简单,直接将Kodein传到工厂即可,不需要instance()
class KodeinApp : Application(), KodeinAware {
override val kodein by Kodein.lazy {
import(androidXModule(this@KodeinApp))
bind() from singleton { KodeinViewModelFactory(kodein) }
bind() from singleton { MyViewModel(kodein) }
}
}
我参考http://kodein.org/Kodein-DI/?6.1/android#view-model-factory使用kodein viewmodel,出现错误。
零件编码
bind<ViewModelProvider.Factory>() with singleton { KodeinViewModelFactory(instance()) }
class KodeinViewModelFactory(private val kodein: Kodein) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T =
kodein.direct.Instance(TT(modelClass))
}
很简单,直接将Kodein传到工厂即可,不需要instance()
class KodeinApp : Application(), KodeinAware {
override val kodein by Kodein.lazy {
import(androidXModule(this@KodeinApp))
bind() from singleton { KodeinViewModelFactory(kodein) }
bind() from singleton { MyViewModel(kodein) }
}
}