gpt4 book ai didi

android - Unresolved 对 Koin 的 sharedViewModel() 的引用

转载 作者:行者123 更新时间:2023-12-02 13:04:15 31 4
gpt4 key购买 nike

我正在尝试将 sharedViewModel() 延迟注入(inject)到我的 Activity 中。出于某种原因,我的 IDE 无法解析引用。它可以成功解析 viewModel() 但不能解析 sharedViewModel()。我可以将它添加到导入中,但我不能使用它。

enter image description here

依赖关系:

def koin_version = "2.0.1"
implementation "org.koin:koin-android:$koin_version"
implementation "org.koin:koin-androidx-scope:$koin_version"
implementation "org.koin:koin-androidx-viewmodel:$koin_version"

我错过了什么吗?

最佳答案

问题是您正在尝试使用 sharedViewModel()Activity .根据documentation :

The Activity sharing its ViewModel injects it with by viewModel() or getViewModel(). Fragments are reusing the shared ViewModel with by sharedViewModel().

要验证,您可以查看FragmentExt.kt的源代码并看到 sharedViewModel()Fragment扩大。所以,而不是 override val viewModel: PaymentViewModel by sharedViewModel()你应该使用 override val viewModel: PaymentViewModel by viewModel()

关于android - Unresolved 对 Koin 的 sharedViewModel() 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58028853/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com