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()
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM