簡體   English   中英

Angular2 rxjs / Subject,rxjs / Observable事件在父子路由器之間不起作用

[英]Angular2 rxjs/Subject , rxjs/Observable events not working across the parent and child router

我想與應用程序內部的組件進行交互/共享數據/相互通信,這些組件具有太多子路由(都基於templateUrl)

嘗試了很多方法,就像事件發射器-@Output()一樣,沒有任何方法適合我。

最后,我知道共享服務可以解決此問題。

我正在尋找這樣的東西

Application >> Global Service
Application >> Router 1 Component  >> Router 1A CompChild >>  Router 1AA
Application >> Router 2 Component  >> Router 2A CompChild>>  Router 2AA
Application >> Router 3 Component  >> Router 3A CompChild >>  **Route 3AA**
$.broadcast('event_name',data); // Router 3AAComponent fire/broadcast/emit event

$allComponents.on('event_name',function(){ .... some event actions ....}): 
// All child component of application can subscribe to that event

這是參考網址: http : //blog.lacolaco.net/post/event-broadcasting-in-angular-2/

請幫我解決這個問題

確保在根組件處僅提供一次GlobalService 如果將其添加到使用該組件的每個組件的提供程序中,則每個組件都會獲得其自己的實例,如果訂閱了一個不同的實例,則會發出事件,因此不會收到通知。

Angular2 DI為每個提供程序維護一個實例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM