【发布时间】:2019-06-25 11:10:41
【问题描述】:
我是 Dagger 2 的新手,并试图在 Kotlin 中实现它。在这里,我试图将我的存储库对象注入到 viewmodel 中。我可以通过这种方式成功注入它
public class LoginViewModel @Inject constructor(var mApplication: Application, var repository: LoginRepository) :
ViewModel() {
这就是我的存储库的样子
class LoginRepository @Inject constructor(val retrofit: APICallInterface) {
这就是我的模块的样子
@Module
class BaseModule {
@Provides
fun getRetrofit(): APICallInterface {
return Retrofit.Builder()
.baseUrl("https://samples.openweathermap.org/data/2.5/")
.addConverterFactory(GsonConverterFactory.create())
.build().create(APICallInterface::class.java)
}
我无法理解的是 Dagger 2 如何能够为存储库提供对象,因为我没有在任何带有 @Provides 注释的模块中提到它。
我已尝试关注许多博客和一些可用的 stckoverflow 问题,但没有一个能解决我的疑问。
任何帮助/解释将不胜感激。
【问题讨论】:
-
你在
@Module类中声明了吗? -
没有。为了清楚起见,添加有问题的模块类。
标签: android mvvm dagger-2 android-viewmodel