3

Dagger 2 能够通过多重绑定注入集合或映射。如何在牙签中做到这一点

例如我想要 @Inject constructor(val map: Map<String, ISyncRepository>)

4

1 回答 1

1

它可能是这样的:

   @Binds
   @IntoMap
   @TheMapKey(“your key”)
   abstract fun getSyncRepository() : ISyncRepository

看以下2个样本

https://google.github.io/dagger/multibindings.html

https://blog.kotlin-academy.com/understanding-dagger-2-multibindings-viewmodel-8418eb372848

为了

@Inject constructor(

     private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards 
         Provider<ViewModel>>

) 

使用:

@Binds
@IntoMap
@ViewModelKey(UserViewModel.class)
abstract ViewModel bindUserViewModel(UserViewModel userViewModel);
于 2018-06-10T13:21:04.437 回答