ฉันสร้างต้นแบบ example.component.html
นั่นอย่างเราได้รับคำขู่แบบนี้เป็นวิธีการเป็นตัวแปรต้องการกระทำเมื่อคลิกเมาส์:
<div>
<button (click)="method()">click here</button>
</div>
บ example.component.ts
แฟ้มจากวิธีการออกมาจากการนำเข้าข้อมูล()ดังนั้นฉันสามารถใช้เอกสารต้นแบบนี้ในหลายสถานการณ์:
@Component({
selector: 'example',
templateUrl: './example.component.html',
})
export class ExampleComponent implements OnInit {
@Input() method;
constructor() {}
ngOnInit(): void {}
}
ที่นี่คือที่ที่มันจะซับซ้อนกว่านี้ ตอนที่พ่อแม่ส่วนประกอบวิธีการนั้นจะถูกกระตุ้นเมื่อมีการคลิกใช้ตัวแปรมานจากการ observable:
parent-example.component.html
<example [method]="onClick"></example>
parent-example.component.ts
@Component({
selector: 'parent-example',
templateUrl: './parent-example.component.html',
})
export class ParentExampleComponent implements OnInit {
@Input() method;
business;
constructor(businessEntityService: BusinessEntityService) {
businessEntityService.entities$.subscribe(
data => (this.business = data),
);
}
onClick() {
console.log(this.business);
}
ngOnInit(): void {}
}
ถึงแม้ว่าพ่อแม่ส่วนประกอบก็คือ subscribing businessEntityService
observable และฉันตรวจสอบว่ามันอยู่ในวามจริงข้อมูลตอนที่ฉันคลิกที่ปุ่มที่คอนโซลปูมบันทึก undefined
.
ฉันเข้าใจเรื่องนี้อาจจะต้องมีส่วนเกี่ยวข้องกับขอบเขตและที่ตั้งกำลังมองหา this.business
ตอนเด็กส่วนประกอบอย่างไรก็ตามฉันอยากจะรู้ถ้ามันเป็นยังไงต้องโทรหาวิธีการนั้นใช้ตัวแปรจากการสมัครสมาชิกจากของส่วนประกอบมือนำเข้าข้อมูล().