Compose 中使用livedata 、viewmodel、协程、获取context

livedata转换成compose 的state,使用需要添加compose livedata的依赖

@Composable
fun StatisticsPage() {
	val dataList by viewModel.dataList.observeAsState(listOf())
}
// compose livedata相关
implementation "androidx.compose.runtime:runtime:$compose_version"
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"

compose组件中获取context

@Composable
fun StatisticsPage() {
	val context = LocalContext.current
}

compose组件中获取viewmodel

@Composable
fun StatisticsPage() {
	val viewModel: StatisticsViewModel = viewModel()
}

compose 中使用协程

@Composable
fun StatisticsPage() {
	 val scope = rememberCoroutineScope()
}
posted @ 2021-12-05 21:11  用户72093285  阅读(2078)  评论(0)    收藏  举报