AngularDart 5:如何获取父组件

时间:2018-05-15 08:42:13

标签: dart angular-dart

在AngularDart 3或4中,我们可以写

@Component(
    selector: 'my-app', 
    template: '<my-child></my-child>', 
    directives: [ChildComponent])
class AppComponent {}

@Component(selector: 'my-child', template: 'The child')
class ChildComponent {
  final AppComponent _parent;

  ChildComponent(this._parent);
}

ChildComponent在构造函数中注入了父级。

当我尝试将此代码转换为AngularDart 5时,出现此错误: No provider found for AppComponent

将此代码转换为AngularDart 5的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

您需要设置父级的visibility以使其可注入

@Component(
  ...,
  visibility:  Visibility.all

或(另见https://github.com/dart-lang/angular/blob/11c05e29fafcea13e4d54f246e70402460777e94/angular/lib/src/core/metadata/visibility.dart#L41

@Component(
  selector: 'parent',
  directives: const [Child],
  providers: const [
    const ExistingProvider(Example, useExisting: Parent),
  ], 

此更改是为了减少生成的代码大小。