2 回答

TA貢獻1796條經驗 獲得超7個贊
我認為你可以利用startWith.
const term$ = new BehaviorSubject('');
const localStorageResults = localStorage.getItem(''); // Map it into the same shape as results$ but the observable unwrapped
const results$ = term$
.pipe(
startWith(localStorageResults),
debounceTime(1000),
switchMap(term =>
getAutocompleteSuggestions(term)
.pipe(
takeUntil(
//skip 1 value
term$.pipe(skip(1))
)
)
)
)
)
你可能不得不修改它,我不確定它是否會很好,debounceTime但這是一個想法。

TA貢獻1831條經驗 獲得超10個贊
所以在處理了幾個小時之后,我發現解決方案非常簡單:
autocomplete(1000, (term => new Observable(s => {
const storageValue = this.fetchFromStorage(term);
s.next(storageValue);
this.fetchFromServer(term)
.subscribe(r => s.next(r));
})))
添加回答
舉報