2017-09-05 198 views
-1

我有一個變量的線程:通信的兩個主題之間

threads: Subject<{[key: string]: Thread }> = new BehaviorSubject({}); 

我想添加這個線程變量,以我的新threadTest變量:

threadTest : Subject<Array<Thread>> = new Subject(); 

是否有可能,如:

this.threadTest = this.threads; 

感謝您的幫助......

+0

不是。因爲'threads'的類型不是'threadTest'。如果兩者都來自同一類型,那麼它就會起作用。 –

+0

'this.threadTest.next(this.threads.getValue());'@ D.Simon這是行不通的? –

+0

@marouanekadiri 它把這個錯誤:屬性「的getValue」不上類型「主題存在<{[鍵:字符串]:螺紋; }>」 – Floriane

回答

1

要從BehaviorSubject的值複製到另一個話題,你需要使用這個 this.threadTest.next(this.threads.getValue());(作爲爲例)

但在這種情況下,如果你使用這個簡單的代碼,你將面臨{[key:string]:Thread}類型的問題不匹配用Thread[]來解決這個問題,你需要把它轉換成這樣的數組:this.threadTest.next((<any>Object).values(this.threads.getVa‌​‌​lue()));