有没有什么方法可以使用注释@Binds代替@Provides,且返回类型需要参数

时间:2019-02-12 09:43:42

标签: android kotlin dagger-2

有任何可能的使用绑定注释的方式提供此功能吗?

@Singleton
@Provides
fun provideUtils(context: Context): Utils {
    return Utils(context)
}

2 个答案:

答案 0 :(得分:0)

如果您的<script src="https://unpkg.com/@reactivex/rxjs@6/dist/global/rxjs.umd.js"></script> <button id="s1">test random</button> <button id="s2">test ajax</button> <button id="s3">test fetch</button>类具有Utils构造函数,例如

@Inject

...那么您可以像这样绑定它,并且Dagger应该能够弄清楚如何创建它(假设您在某处提供了class Utils @Inject constructor(private val context: Context) { // ... } ):

Context

答案 1 :(得分:0)

您应该在此处使用构造函数注入。如果使用构造函数注入,则不需要@Provides @Binds

@Singleton // scope on the class
class Utils @Inject constructor(private val context: Context)

仅此而已。