1
我有一個父組件:如何獲得父組件參考
<Parent>
<Child (click)=doSomething()></Child>
</Parent>
我希望有父組件的引用,這樣我可以在該方法中通過它,將觸發一個事件,將有一個父編號:
doSomething() {
// I would like to trigger the event, that would be catch elsewhere in the app
trigger(new ClickedEvent(referenceToParent));
}
的情況下會是這樣:
class ClickedEvent {
constructor (public referenceToParent: Parent) {}
}
我將如何做到這一點?
其實,我不想在父組件中處理它。我想在代碼中的其他地方處理它:( –
@JerzyGruszka爲什麼不製作可以爲事件廣播和註冊偵聽器的服務? – toskv
我有,但是如何將引用傳遞給特定實例? –