簡體   English   中英

如何將 Fragment 的參數項添加到 Koin 依賴圖中?

[英]How to add a Fragment's argument item to Koin dependency graph?

我有一個ViewModel ,它有一個依賴項,它應該取自Fragmentarguments

所以它是這樣的:

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取自這就是Fragmentarguments ,以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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM