[英]How to add a Fragment's argument item to Koin dependency graph?
我有一个ViewModel
,它有一个依赖项,它应该取自Fragment
的arguments
。
所以它是这样的:
class SomeViewModel(someValue: SomeValue)
现在的片段临危的SomeValue
在其arguemnt像这样:
class SomeFragment : Fragment() {
val someViewModel: SomeViewModel by viewModel()
companion object {
fun newInstance(someValue: SomeValue) = SomeFragment().apply {
arguments = bundleof("someKey" to someValue)
}
}
}
问题是我不知道如何添加SomeValue
取自这就是Fragment
的arguments
,以Koin
的模块。
有没有办法让片段对 Koin 依赖图有所贡献?
所以对于问同样问题的其他人,这里是答案:
https://doc.insert-koin.io/#/koin-core/injection-parameters
所以基本上,
你可以像这样创建你的模块:
val myModule = module {
viewModel { (someValue : SomeValue) -> SomeViewModel(someValue ) }
}
现在在您的片段中,您可以执行以下操作:
class SomeFragment : Fragment() {
val someViewModel: SomeViewModel by viewModel {
parametersOf(argument!!.getParcelable<SomeValue>("someKey"))
}
companion object {
fun newInstance(someValue: SomeValue) = SomeFragment().apply {
arguments = bundleof("someKey" to someValue)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.