App-Component-Html:(在 Index.Html 中為<app-root></app-root>)
<h2> {{currentOrNextWeek}} week from {{dateMonday}} to {{dateFriday}} </h2>
<div class="container"> <router-outlet></router-outlet> </div>
Custom-Component-TS:在路由 -> router.module.ts 中定義,除了許多其他自定義組件之外,它還有一個表,顯示由來自服務的資料實作的資訊,該服務將獲取請求發送到 API。像這樣:
this.configService.getData.subscribe(data => organizeData(data))
資料本身具有諸如“Week = Current”、“DateOfMonday=01.01.2021”之類的資訊(一切都是字串)
如何將資料從 custom.component 傳遞到 app.component?我試過 EventEmitter 但我使用<router-outlet>. 正如我所理解的,序列以 app-component 開頭,但第一個 GetData 呼叫在自定義組件中,但標題在 app-component-html 中
uj5u.com熱心網友回復:
您的問題似乎是如何在兩個動態且不需要是父子組件的組件之間交換屬性。在這種情況下,通過 Service 進行通信似乎是最好的方法。
您可以創建一個AppCommunication使用屬性呼叫的服務,該服務tittle = new subject()可以在 app 組件處訂閱 observable,并且可以注入到您的自定義組件中,以便您可以從“next”觸發值。
這可以在您想要設定它的任何地方使用。
uj5u.com熱心網友回復:
您不能使用 RXJS 發送此資料,behaviorSubject?
在某些服務上:
varbSubject$ = new BehaviorSubject(<string>);
在您的應用組件上:
bSubject$ = yourservice.varbSubject$
bSubject$.next("b");
在自定義未知元素上 =>
bSubject$ = yourservice.varbSubject$
bSubject$.subscribe(value => {
console.log("Subscription got", value);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360620.html
