这是我目前拥有的并且有效:
@FragmentScope
@Component(dependencies = {FacebookComponent.class},
modules = {FragmentFacebookLoginModule.class})
public interface FragmentFacebookLoginComponent {
void inject(FragmentFacebookLogin fragment);
}
现在我想添加另一个依赖项。我把它改成了这样:
@Component(dependencies = {FacebookComponent.class, AnotherComponent.class},
modules = {FragmentFacebookLoginModule.class})
但现在我收到此错误消息:
FragmentFacebookLoginComponent depends on more than one scoped component
我该如何解决这个问题?我怎样才能拥有多个依赖项?
如果我从一个组件中删除范围,我会收到此错误消息:
AnotherComponent (unscoped) cannot depend on scoped components
最佳答案
我在这里找到了答案:https://stackoverflow.com/a/29619594/1016472
最后,我创建了一个具有正确作用域的 AppComponent,并让 FacebookComponent 和 AnotherComponent 扩展了这个 AppComponent。
FacebookComponent 和 AnotherComponent 没有自己的范围(我删除了它)。
现在看起来像这样:
@AppScope
@Component
public interface AppComponent {
}
@Component(modules = {FacebookModule.class})
public interface FacebookComponent extends AppComponent {
}
@Component(modules = {AnotherModule.class})
public interface AnotherComponent extends AppComponent {
}
@FragmentScope
@Component(dependencies = {FacebookComponent.class, AnotherComponent.class},
modules = {FragmentFacebookLoginModule.class})
public interface FragmentFacebookLoginComponent {
void inject(FragmentFacebookLogin fragment);
}
关于java - 具有多个依赖项的 Dagger2 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30418644/