由於超出此問題範圍的原因,我不得不停止使用@injectable
。我轉換了使用它的服務,只是手動創建它們而不是使用@injectable
,它適用於除我的服務之外的所有服務,使用subscribe
我必須刪除@injectable
之前使用訂閱的服務確實有效。我可以在沒有@injectable
的情況下使用它嗎?Angular 2訂閱不工作
代碼:
服務:
import { Subject } from 'rxjs/Subject';
export class ResultSelectionService {
// Observable string sources
private resultSelectedSource = new Subject();
// Observable string streams
resultSelected$ = this.resultSelectedSource.asObservable();
// Service message commands
selectResult(result: any, place: any) {
this.resultSelectedSource.next({result: result, place: place});
}
}
服務用戶(調用selectResult功能不執行 - 良):
應該執行,但不//arrow function because when called above "this" is an html element
resultClick = (event) => {
this.resultSelectionService.selectResult(event.data.result, event.data.place);
}
方法(代碼永遠不會內result => {
:
constructor() {
this.resultSelectionService = new ResultSelectionService();
this.resultSelectionService.resultSelected$.subscribe(
result => {
this.selectedResult = { result };
$('#result-details-modal').appendTo("body").modal('show');
$('#result-details-modal').on('shown.bs.modal', (e) => {
this.resizeMap();
this.canGetAddress = true;
this.addMarkersToMap(this.map);
//this.bindAutoCompleteToMap();
})
console.log(this.selectedResult.result);
});
}
我可以在不使用@injectable
的情況下獲得訂閱的功能嗎?如果沒有,我該如何解決這個問題,創造類似的東西?
這些東西是完全無關的。您應該嘗試修復您的設置。不知道@Injectable()是否會導致任何問題。 –
「selectResult」的調用者和「resultSelected $ .subscribe」的調用者之間的關係在你的問題中沒有完全清楚。它們是否在同一個組件中?你不得不停止使用'@ Injectable'的陳述是關於並表明問題在別處。 – cartant
@GünterZöchbauer你有什麼建議我確定我的設置?我試過你的唯一建議 – BeniaminoBaggins