Android - Dagger注入方法里面

问题描述 投票:-1回答:1

在我的应用程序中使用dagger,我想在方法中注入类,但它显示编译时错误

fun someMethod(){
  @Inject
  val testClass : TestClass
 } 

它显示我的错误

This annotation is not applicable to local variable

任何帮助如何处理这个?

android kotlin dagger-2 dagger
1个回答
0
投票

注入元素在活动或片段中应该是全局的

@Inject
lateint var testClass : TestClass  //global declaration


fun someMethod(){
testClass.dosomething()
} 
© www.soinside.com 2019 - 2024. All rights reserved.