Android Compose State 文档中的 "viewModel()" 方法来自哪里?

Where is the "viewModel()" method in Androids Compose State documentation coming from?

Androids compose / state documentation 包含一个引用神秘的 viewModel() 函数或方法的示例,但由于文档已遗漏所有 import 语句,我终究无法弄清楚这个函数在哪里来自..

显然在放弃并发布问题后 2 分钟就弄明白了。

你需要这个依赖:

androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha05

您可以查看 official doc:

you can access a ViewModel from any composable by calling the viewModel() function.

To use the viewModel() functions,add the androidx.lifecycle:lifecycle-viewmodel-compose:$latestVersion dependency to your build.gradle file.

最后使用:

import androidx.lifecycle.viewmodel.compose.viewModel