找不到viewModels,viewModels()

谷歌官方文档上学习mvvm,当我写到private val model: UserViewModel by viewModels()时,viewModels()爆红,查了资料,找到gradle,添加依赖

dependencies{
    implementation "androidx.activity:activity-ktx:1.2.0"//新添加
    implementation "androidx.fragment:fragment-ktx:1.3.0"
 
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'//我的文件已有的
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
 
}

  

posted on 2022-04-08 12:01  巫山老妖  阅读(219)  评论(0编辑  收藏  举报