Angular @查询未找到组件引用

Angular @查询未找到组件引用,angular,typescript,Angular,Typescript,我创建了一个在内部使用子组件的父组件。我的目标是让父组件使用我的子组件API,但我遇到了麻烦。下面是我对相关领域的代码进行了细化/浓缩 class TripStop {} @Component({ selector: 'route-map' }) @View({ template: '...' }) class RouteMap { setRoute(route: TripStop[]) { // Set the route! } } @Component({

我创建了一个在内部使用子组件的父组件。我的目标是让父组件使用我的子组件API,但我遇到了麻烦。下面是我对相关领域的代码进行了细化/浓缩

class TripStop {}

@Component({
  selector: 'route-map'
})
@View({
  template: '...'
})
class RouteMap {

  setRoute(route: TripStop[]) {
    // Set the route!
  }

}

@Component({
  selector: 'route-plan'
})
@View({
  template: `
    <div>
      ...
      <route-map></route-map>
    </div>
  `,
  directives: [RouteMap]
})
class RoutePlan {

  private stops: TripStop[];
  private maps:QueryList<RouteMap>;

  constructor(@Query(RouteMap) maps:QueryList<RouteMap>) {
      this.maps = maps;
    }

  public addStop() {
    this.stops.push(new TripStop());

    var routeMapComponent = this.maps.first; // This evaluates to undefined.
    routeMapComponent.setRoute(this.stops);
  }
}
类TripStop{}
@组成部分({
选择器:“路线图”
})
@看法({
模板:“…”
})
类路由映射{
设置路线(路线:TripStop[]){
//设定路线!
}
}
@组成部分({
选择器:“路线计划”
})
@看法({
模板:`
...
`,
指令:[路由映射]
})
类路由规划{
私人站点:TripStop[];
私有地图:QueryList;
构造函数(@Query(RouteMap)映射:QueryList){
this.maps=maps;
}
公共addStop(){
this.stops.push(新TripStop());
var routeMapComponent=this.maps.first;//此计算结果为未定义。
routeMapComponent.setRoute(this.stops);
}
}
到目前为止,我无法在我的计划路线组件中获得对子路线图组件的引用。从上所读到的内容来看,似乎我应该能够使用
@Query
装饰器向与查询匹配的任何子组件注入引用(作为
ComponentRef
对象)

我尝试在查询
@query(RouteMap)
中使用组件类型,并添加对我的映射元素
的引用,然后使用该
@query('myMap')
进行查询

令人困惑的是,我设法让
@ViewQuery
装饰器工作,并通过向我的元素添加一个引用来返回对underly-map-DOM元素的引用&如上所述使用该元素进行查询。这并没有真正帮助我,因为我更愿意通过组件的API与组件通信,而不是修改它的底层DOM——然而,我认为这表明我的应用程序的一般连接工作正常

我错过了什么


编辑:我正在使用版本
2.0.0-alpha.37

从版本
2.0.0-alpha.37
开始,要获取对子指令的引用,可以使用
@ViewQuery
装饰器,传递子组件的类型。要获取子DOM元素,可以使用
@ViewQuery
装饰器传递对该元素的引用

@Component({
  selector: 'Child'
})
@View({
  template: '<h1>I'm a child</h1>'
})
class Child {}

@Component({
  selector: 'Parent'
})
@View({
  template: '<child #myComponent></child>',
  directives: [Child]
})
class Parent {

  private childDirectives:QueryList<Child>;
  private childViews:QueryList<Child>;

  constructor(
    @ViewQuery(Child) childDirectives:QueryList<Child>,
    @ViewQuery('myComponent') childViews:QueryList<ElementRef>
  ) {
    // A query list of Child components.
    this.childDirectives = childDirectives;
    // A query list of views with the id #myComponent.
    this.childViews = childViews;
  }
}
@组件({
选择器:“孩子”
})
@看法({
模板:“我是个孩子”
})
类子{}
@组成部分({
选择器:“父级”
})
@看法({
模板:“”,
指令:[儿童]
})
班级家长{
私有儿童指令:QueryList;
私有儿童视图:QueryList;
建造师(
@ViewQuery(子)子指令:QueryList,
@ViewQuery('myComponent')子视图:QueryList
) {
//子组件的查询列表。
this.childdirections=childdirections;
//id为#myComponent的视图的查询列表。
this.childview=childview;
}
}
我不确定
@Query
的角色是什么,也不确定
@ViewQuery
是否接受ID以外的任何其他选择器。这种行为似乎没有被很好地记录下来,而且似乎有点不直观——但是,看起来API在这方面仍在变化()