0
想實現可觀察到下面的類實施觀察的失敗,因爲離子2忘記觀察
import {Injectable, } from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/share';
import {Observer} from 'rxjs/Observer';
@Injectable()
export class CalendarService {
dataChange: Observable<any>;
dataChangeObserver: Observer<any>;
constructor() {
this.dataChange = new Observable((observer) => {
this.dataChangeObserver = observer;
}).share();
}
setDate(date: any) {
this.dataChangeObserver.next(date);
}
}
當調用setDate()
與價值正在以下錯誤
ORIGINAL EXCEPTION: TypeError: Cannot read property 'next' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property 'next' of undefined
看來,觀察dataChangeObserver
未定義。但是在調試時我可以看到我設置的值爲dataChangeObserver
。但是,當撥打setDate(date: any)
時,Ionic 2會以某種方式忘記它。