【问题标题】:Unresolved reference to Koin's sharedViewModel()未解决对 Koin 的 sharedViewModel() 的引用
【发布时间】:2019-09-20 12:58:06
【问题描述】:

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

依赖关系:

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"

我错过了什么吗?

【问题讨论】:

    标签: android kotlin dependency-injection koin


    【解决方案1】:

    问题是您试图在Activity 中使用sharedViewModel()。根据documentation

    共享其 ViewModel 的 Activity 通过 viewModel() 或 获取视图模型()。片段正在重用共享的 ViewModel 与 by sharedViewModel()。

    要验证,您可以查看FragmentExt.kt 的源代码,发现sharedViewModel()Fragment 扩展名。因此,您应该使用 override val viewModel: PaymentViewModel by viewModel() 而不是 override val viewModel: PaymentViewModel by sharedViewModel()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多