简体   繁体   English

在 Android ViewModel 中注入依赖项?

[英]Injecting dependencies in a Android ViewModel?

Hi i have a ViewModel that has two var's that get injected using Dagger 2.11.嗨,我有一个 ViewModel,它有两个使用 Dagger 2.11 注入的 var。

However it never gets injected in my viewModel whilst everywhere else in my app, these same dependecy Vars get initialised and injected perfectly然而,它从来没有被注入我的 viewModel 而在我的应用程序的其他地方,这些相同的依赖变量被初始化并完美注入

Below is my viewModel下面是我的视图模型

class MyViewModel : AndroidViewModel, MyViewModelContract {


    private lateinit var pointsBalance: MutableLiveData<PointsBalance>


    @Inject
    lateinit var accountDelegator: AccountDelegatorContract



@Inject
constructor(application: Application) : super(application)

init {
    DaggerApplicationComponent.builder().application(getApplication() as MyApplication).build().inject(this)
}

    override fun getPointsBalance(): LiveData<PointsBalance> {
        if (!this::pointsBalance.isInitialized) {
           //get balance from network api etc
        }

        return pointsBalance
    }

accountDelegator complains that it is not initialised accountDelegator 抱怨它没有初始化

Below is how i bind this viewModel inside MyModule.class下面是我如何在 MyModule.class 中绑定这个 viewModel

   @Provides
    @JvmStatic
    @Singleton
    fun providesViewModel(viewModel: MyViewModel): MyViewModelContract = viewModel

my custom application我的自定义应用程序

class MyApplication : DaggerApplication() {
    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        val applicationComponent = DaggerApplicationComponent.builder()
                .application(this)
                .build()
        applicationComponent.inject(this)
        return applicationComponent
    }

}

my applicationComponent我的应用程序组件

@Singleton
@Component(modules = arrayOf(MyModule::class,
        ))
interface ApplicationComponent : AndroidInjector<DaggerApplication> {
    fun inject(mApplication: MyApplication)
    override fun inject(instance: DaggerApplication)

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(applicaton: MyApplication): Builder
        fun build(): ApplicationComponent
    }


}

How i use this viewmodel in a activity/fragment我如何在活动/片段中使用此视图模型

viewModel = ViewModelProviders.of(this).get(MyViewModel::class.java)

I believe issue is that you're not calling inject .我相信问题是你没有打电话给inject You'd need for a start to add following to ApplicationComponent您需要开始向ApplicationComponent添加以下内容

void inject(MyViewModel viewModel);

What I'm doing here then fwiw is inheriting from AndroidViewModel (which gives access to application instance) then calling following in ViewModel class (you'd need to substitute DaggerProvider.getComponent with however you're accessing component in your code)我在这里做的然后 fwiw 是从AndroidViewModel继承(它可以访问应用程序实例),然后在 ViewModel 类中调用以下内容(您需要替换DaggerProvider.getComponent ,但是您正在访问代码中的组件)

init {
    DaggerProvider.getComponent(application).inject(this)
}

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

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