java - 具有多个依赖项的 Dagger2 组件

标签 java android dagger-2

这是我目前拥有的并且有效:

@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/

相关文章:

android - 如何正确地为单个 Activity App 注入(inject) fragment

Java:将可抛出/异常的整个堆栈跟踪转换为 ByteBuffer 的最有效方法?

大量迭代后,Java while 循环会随着时间的推移而显着变慢

Android 启用和禁用 GPS

java - 没有在 Spring Controller 方法中获取 JSON 值

java - 按下android按钮后的 Activity 生命周期 "recents"创建新 Activity 但不销毁旧 Activity

java - JScrollPane 缩放 View ,里面有组件

java - Android studio [2.1]不显示 "Import class"

java - 如何使用 Dagger2 将 FragmentPagerAdapter 正确注入(inject)我的 Activity 类?

java - 为什么 Dagger 2 中需要@SubComponent?