在角度

时间:2018-11-12 23:55:12

标签: angular

//first approach importing the child component and accessing its properties 

import { childComponent}  from './childdetail.component';
@Component({
  selector: "testProject",
  templateUrl: "app/partials/Main.html"
})
export class AppComponent { 
  @ViewChild(childComponent) childDetail:childDetailComponent;
  this.getChildProperty(this.childDetail.shipment)

  getChildProperty(value) {
    console.log(value);
  }
}



//second approach include getChildProperty method in ts file
//child component will have an eventEmitter that emits shipment value
// view will look like below code

<child-component (shipmentValue)="getChildProperty($event)">

两者中哪个是更好的方法,为什么?我对哪种方法最好感到困惑,因为我可以同时实现两者的最终目标。任何解释将不胜感激。预先感谢

1 个答案:

答案 0 :(得分:0)

链接的文章将为您提供一些指导: https://blog.angular-university.io/angular-viewchild/

为了使代码库具有可读性和模块化,需要大多数时间事件发射器以及组件之间的简单交互。需要注入对该组件的引用时,请使用viewchild。