如何通过koin中的参数传递界面
我在Koin的初学者。
我有一种名为“ mesherepoimpl”的方法,将接口作为参数。
我知道我无法将接口传递到Koin中的方法,因此我创建了一个类并从接口中扩展了该类,然后在Koin模块中添加了该类,因此我将类用作MesherePoimpl的参数。
但是Android Studio给了我这个错误:
由:org.koin.core.error.nobeandeffoundexception:| - 类别没有定义:'com.app.meshe.data.repo.mesherepo'。检查您的定义!
这是我的DI模块:
val mesheModule =
module {
single { getInstance(androidContext()) }
single { MesheLocalDataSource() } //*
single { MesheRepoImpl(get()) } //**
factory { TaskViewModelFactory(get()) }
viewModel { TaskViewModel(get()) }
viewModel { RewardViewModel(get()) }
viewModel {MainViewModel()}
}
1星线是我的类,它从接口延伸,2星线是将接口作为参数的类。
如果我不能使用类,该如何将接口作为参数传递?
I'm so beginner in koin.
I have a method that named "MesheRepoImpl" that get an interface as parameter.
I know that I can't pass an interface to a method in Koin, so I created a class and extends that from the interface then I added that class in koin module, so I use the class as parameter for MesheRepoImpl.
But android studio gives me this error:
Caused by: org.koin.core.error.NoBeanDefFoundException: |- No definition found for class:'com.app.meshe.data.repo.MesheRepo'. Check your definitions!
This is my Di module:
val mesheModule =
module {
single { getInstance(androidContext()) }
single { MesheLocalDataSource() } //*
single { MesheRepoImpl(get()) } //**
factory { TaskViewModelFactory(get()) }
viewModel { TaskViewModel(get()) }
viewModel { RewardViewModel(get()) }
viewModel {MainViewModel()}
}
The 1 star line is my class that extends from the interface and the 2 stars line is the class that get interface as parameter.
How can I pass the interface as parameter, if I can't use a class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于仍然没有答案,我建议您考虑继续
您的
所以,只需实现
MeshoRepo
而不是将其作为参数传递给MeshoRepoImpl
。尝试直接回答您的问题,您可以在 Koin 模块中定义接口并传递它们,但您还必须提供它们的实现:
并且,请不要忘记接口不是对象。
Since there's still no answer, I'd advise you to consider going with
over your
So, just implement
MeshoRepo
over passing it as an argument toMeshoRepoImpl
.Trying to answer directly your question, you are able to define interfaces in Koin module and pass them, but you have to provide their implementations, as well:
And, please, do not forget that an interface is not an object.