需要访问属性子元素.

<div>
   <shipment-detail #myCarousel ></shipment-detail>
</div>
@Component({
  selector: "testProject",
  templateUrl: "app/partials/Main.html",
  directives: [ShipmentDetail] })
class AppComponent { 
  getChildrenProperty() {
  // I want to get access to "shipment" 
  }
}

children :

@Component({
  selector: "shipment-detail",
}) 
export class ShipmentDetail  {
  shipment: Shipment;
}

推荐答案

Component Interaction cookbook.因此,使用@ViewChild()并向ShipmentDetail添加一个方法,父级可以调用该方法来检索装运值,或者直接访问该属性,如下所示(因为我很懒,不想编写API/方法):

@Component({
  selector: "testProject",
  templateUrl: "app/partials/Main.html",
  directives: [ShipmentDetail] 
})
export class AppComponent { 
  @ViewChild(ShipmentDetail) shipmentDetail:ShipmentDetail;
  ngAfterViewInit() {
      this.getChildProperty();
  }
  getChildProperty() {
     console.log(this.shipmentDetail.shipment);
  }
}

Plunker

Typescript相关问答推荐

推断类型

创建一个将任意命名类型映射到其他类型的TypScript通用类型

如何使打包和拆包功能通用?

TypeScript泛型参数抛出错误—?&#"' 39;预期"

动态调用类型化函数

对深度对象键/路径进行适当的智能感知和类型判断,作为函数参数,而不会触发递归类型限制器

如何键入函数以只接受映射到其他一些特定类型的参数类型?

如何在单击停止录制按钮后停用摄像机?(使用React.js和Reaction-Media-Recorder)

与字符串文字模板的结果类型匹配的打字脚本

使某些(嵌套)属性成为可选属性

如何在TypeScrip中正确键入元素和事件目标?

<;T扩展布尔值>;

是否存在类型联合的替代方案,其中包括仅在某些替代方案中存在的可选属性?

使用TypeScrip的类型继承

有没有办法防止类型交集绕过联合类型限制?

使用强类型元组实现[Symbol.iterator]的类型脚本?

TS不能自己推断函数返回类型

为什么`map()`返回类型不能维护与输入相同数量的值?

是否使用类型将方法动态添加到类?

Svelte+EsBuild+Deno-未捕获类型错误:无法读取未定义的属性(读取';$$';)