Bir şablon oluşturdum example.component.html
bu, tıklama eylemine değişken olarak bir yöntem alır:
<div>
<button (click)="method()">click here</button>
</div>
on the example.component.ts
dosya, yöntem bir input () öğesinden gelir, böylece bu şablonu birden çok durumda kullanabilirim:
@Component({
selector: 'example',
templateUrl: './example.component.html',
})
export class ExampleComponent implements OnInit {
@Input() method;
constructor() {}
ngOnInit(): void {}
}
İşte burada işler karışıyor. Üst bileşende, tıklandığında tetiklenecek yöntem gözlemlenebilir bir değişkenden gelen bir değişkeni kullanır:
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 {}
}
Üst bileşen abone olsa bile businessEntityService
gözlemlenebilir ve aslında verilere sahip olduğunu kontrol ettim, düğmeye tıkladığımda konsol günlükleri undefined
.
Bunun muhtemelen kapsamla ilgisi olduğunu ve yığının aradığını anlıyorum this.business
bununla birlikte, children bileşeninde, kendi bileşeninden bir abonelikten bir değişkeni İnput () olarak kullanan bir yöntemi çağırmak için herhangi bir yöntem olup olmadığını bilmek isterim.