简体   繁体   English

使用 Dagger 2 将 ViewModel 注入 Fragment,带参数

[英]Injecting ViewModel into the Fragment with Dagger 2, with parameters

I'm trying to use a single factory for creating ViewModels with constructor parameters.我正在尝试使用单个工厂来创建带有构造函数参数的 ViewModel。 I followed the recommendation in one of the open dagger issues by @matejdro https://github.com/google/dagger/issues/1273我遵循了@matejdro https://github.com/google/dagger/issues/1273在其中一个开放匕首问题中的建议

But the injection doesn't work.但是注射不起作用。

I"m getting an error:"error: [Dagger/MissingBinding] kotlin.Lazy cannot be provided without an @Provides-annotated method."我收到一个错误:“错误:[Dagger/MissingBinding] kotlin.Lazy 不能在没有 @Provides 注释的方法的情况下提供。”

Any idea how to resolve the issue?知道如何解决这个问题吗? I uploaded my created project to github: https://github.com/davida5/daggerViewModelBestPractice我将我创建的项目上传到 github: https : //github.com/davida5/daggerViewModelBestPractice

That's pretty simple, you should be using dagger.Lazy (include an import for it) instead of the kotlin.Lazy这很简单,您应该使用dagger.Lazy (包括导入)而不是kotlin.Lazy

Once that is fixed you will run into another issue though;一旦解决了这个问题,你就会遇到另一个问题; AFAIK you can't use a qualified dependency without a corresponding qualified provider. AFAIK 如果没有相应的合格提供者,您就不能使用合格的依赖项。 You have a qualified UserManager instance being injected into AccountsViewModel .您有一个合格的UserManager实例被注入AccountsViewModel Either add a qualified provider method or remove the qualification from the AccountsViewModel .添加合格的提供者方法或从AccountsViewModel删除限定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM