使用 Koin 注入变量更改值

Change value with Koin injected variable

我需要为我的单个 Koin 变量分配一个新值,但 Koin 不允许在注入的变量上使用 var...

private val userAssets: UserAssets by inject()

如何设置 userAssets 的值? 或者还有其他事情要做,使 UserAssets 成为单例?

val dataModule = module {
    factory {
        RetrofitBuilder(
            androidContext()
        )
    }
    single { LoginCredential() }
    single { UserAssets() }
}

我个人不使用Koin,所以无法验证,但是Koin也提供了直接用get()获取实例的方法。所以在你的情况下:

private var userAssets: UserAssets = get()

可能有用,所以您可以稍后重新分配它,至少这是我从他们的 documentation 那里了解到的。

您好,您可以尝试声明注入参数,这里是 reference

在模块中你可以这样写:

single { value : String -> UserAssets(value) }

那么你应该可以使用 inject

val userAssets : UserAssets by inject { parametersOf("value") }