【问题标题】:DI - No provider found for EventBusDI - 找不到 EventBus 的提供程序
【发布时间】:2014-10-24 22:44:43
【问题描述】:

使用 Angular Dart,我定义了一个这样的事件总线:

class MyModule extends Module {

    MyModule() {
        bind(EventBus, toImplementation: EventBus);
        ...
    }
}

当我想通过简单的操作将该事件总线注入到组件中时:

class MyComponent {

    final EventBus _eventBus;

    MyComponent(this._eventBus) {}
}

我收到错误:

No provider found for EventBus!

我不知道如何调试这个......

事件总线是一个外部库,看起来像:

library event_bus;

import 'dart:async';

@MirrorsUsed(symbols: '*') // Do not keep any names.
import 'dart:mirrors';

class EventBus {

  StreamController _streamController;

  EventBus({bool sync: false}) {
    _streamController = new StreamController.broadcast(sync: sync);
  }
  ...
}

欢迎任何帮助...谢谢!

【问题讨论】:

标签: dependency-injection dart angular-dart


【解决方案1】:

我有点晚了,但在这种情况下——当你不能轻易地在类上放置 @Injectable() 注释时——最简单的解决方案是给注入器一个值:

bind(EventBus, toValue: new EventBus());

【讨论】:

  • 可能是更好的方法,因为您可能每次都想要相同的实例。 toImplementation 将创建一个新实例。另一方面,Angular 有类似 EventBus 的内置功能。你真的需要自定义事件总线吗...
猜你喜欢
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
  • 2023-01-31
  • 2021-12-15
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多