具有多个依赖项的Dagger2组件 [英] Dagger2 component with more than one dependencies

查看:338
本文介绍了具有多个依赖项的Dagger2组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is what I currently have and it works:

@FragmentScope
@Component(dependencies = {FacebookComponent.class}, 
           modules = {FragmentFacebookLoginModule.class})
public interface FragmentFacebookLoginComponent {

    void inject(FragmentFacebookLogin fragment);
}

现在我想添加另一个依赖项。我把它更改为:

Now I want to add another dependency. I changed it to this:

@Component(dependencies = {FacebookComponent.class, AnotherComponent.class}, 
           modules = {FragmentFacebookLoginModule.class})

但现在我收到此错误消息:

But now I get this error message:


FragmentFacebookLoginComponent依赖于多个作用域的
组件

FragmentFacebookLoginComponent depends on more than one scoped component

我该如何解决这个问题?我如何拥有多个依赖项?

How can I solve this? How can I have more than one dependencies?

如果我从一个组件中删除范围,我会收到以下错误消息:

If I remove the scope from one component I get this error message:


AnotherComponent(unscoped)不能依赖于作用域组件

AnotherComponent (unscoped) cannot depend on scoped components


推荐答案

<我在这里找到了答案: https://stackoverflow.com/a/29619594/1016472

最后,我创建了一个具有正确范围的AppComponent,让FacebookComponent和AnotherComponent扩展这个AppComponent。

At the end I created a AppComponent with the right scope and let FacebookComponent and AnotherComponent extends this AppComponent.

FacebookComponent和AnotherComponent没有拥有它自己的范围(我将其删除)。

FacebookComponent and AnotherComponent does not have it's own scope (I removed it).

现在看起来像这样:

@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);
}

这篇关于具有多个依赖项的Dagger2组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆