小编Bij*_*han的帖子

Viewmodel 在 Compose 中实例化,并多次调用 hiltviewmodel()

我正在尝试打开一个撰写详细信息页面,其中使用 hiltviewmodel() 添加了 Viewmodel 该视图模型具有分页数据状态和 API 调用结果状态,需要它来显示更新的数据以及列表。但是当我导航到详细信息页面时,viewmodel 方法被多次调用,甚至 init {} 块也被调用。有人遇到过类似的问题吗,或者有什么解决办法吗?

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun DashboardScreen(
    navController: NavController,
    dashboardViewModel: DashboardViewModel = hiltViewModel(),
    Name: String = ""
) {
    val lazyArticleList = dashboardViewModel.articlePages.collectAsLazyPagingItems()
    val dashState = dashboardViewModel.dashboardState.value
}
Run Code Online (Sandbox Code Playgroud)

android android-viewmodel android-jetpack-compose

8
推荐指数
1
解决办法
2057
查看次数