找不到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'

}

便可解决。

这是小睿的博客,如果需要转载,请标注出处啦~ヾ(≧▽≦*)o谢谢。
原文地址:https://www.cnblogs.com/Yunrui-blogs/p/14784766.html