我想從a Service
中調用我的Component
中的方法。什麼是正確的方法來做到這一點?我曾嘗試使用rxjs Subject
來創建Observable,但我無法啓動它。Angular2從服務中調用組件
import {Subject} from 'rxjs/Subject';
export class MyService {
callComponent = function(value) {
let invokeEvent = new Subject();
invokeEvent.next({some:value})
}
}
,並在我的組件
export class MyComponent {
constructor(private _myService: MyService) {
this._myService.invokeEvent.subscribe(value => console.log(value))
}
}
我不能得出臨時變量'invokeEvent'在服務和對象'之間的關係this._myService .invokeViewEvent',你在組件中使用。 –
抱歉錯字。這是我想要聽的invokeEvent。當我調用'callComponent'時,我想讓組件觸發。我被困在觀察對象中。 – Rob