Angular 6 將會把 RxJS 一起升級到 6 版,在 RxJS 6 版有許多的 breaking changes,這些 breaking changes 有許多是為了簡化開發時的寫法所做的改變,所以不要太緊張,就慢慢的了解就可以了。
RxJS 6 Breaking Changes 列表
- webSocket:
webSocketcreator function now exported fromrxjs/websocketaswebsocket. - IteratorObservable: IteratorObservable no longer share iterator between subscription
- utils: Many internal use utilities like
isArrayare now hidden underrxjs/internal, they are implementation details and should not be used. - testing observables:
HotObservableandColdObservable, and other testing support types are no longer exported directly. - creation functions: All create functions such as of, from,
combineLatestandfromEventshould now be imported fromrxjs/create. - types and interfaces: Can no longer explicitly import types from
rxjs/interfaces, import them fromrxjsinstead - symbols: Symbols are no longer exported directly from modules such as
rxjs/symbol/observableplease useSymbol.observableandSymbol.iterator(polyfills may be required) - deep imports: Can no longer deep import top-level types such as
rxjs/Observable,rxjs/Subject,rxjs/ReplaySubject, et al. All imports should be done directly fromrxjs, for example:import \{ Observable, Subject \} from 'rxjs'; - schedulers: Scheduler instances have changed names to be suffixed with
Scheduler, (e.g.asap->asapScheduler) - operators: Pipeable operators must now be
imported from rxjslike so:import { map, filter, switchMap } from 'rxjs/operators';. No deep imports. - ajax: Ajax observable should be imported from
rxjs/ajax. - ajax: will no longer execute a CORS request by default, you must opt-in with the crossDomain flag in the config.
- Observable: You should no longer deep import custom Observable implementations such as
ArrayObservableorForkJoinObservable. - _throw: _throw is now exported as
throwError - operators: Deep imports to
rxjs/operator/*will no longer work. Again, pipe operators are still where they were. - error handling: Unhandled errors are no longer caught and rethrown, rather they are caught and scheduled to be thrown, which causes them to be reported to window.onerror or process.on(『error』), depending on the environment. Consequently, teardown after a synchronous, unhandled, error will no longer occur, as the teardown would not exist, and producer interference cannot occur
- distinct: Using
distinctrequires aSetimplementation and must be polyfilled in older runtimes - asap: Old runtimes must polyfill Promise in order to use ASAP scheduling.
- groupBy: Older runtimes will require Map to be polyfilled to use
groupBy - TypeScript: IE10 and lower will need to polyfill
Object.setPrototypeOf - operators removed: Operator versions of static observable creators such as
merge,concat,zip,onErrorResumeNext, andracehave been removed. Please use the static versions of those operations. e.g.a.pipe(concat(b, c)) becomes concat(a, b, c). - rxjs:
rxjs/createitems are now exported fromrxjs - throwError: Observable.throw no longer available in TypeScript without a cast
- empty:
empty()without a scheduler will return the same instance every time. - empty: In TypeScript,
empty()no longer accepts a generic argument, as it returnsObservable<never> - never:
never()always returns the same instance - never: TypeScript typing for
never()is nowObservable<never>and the function no longer requires a generic type. - never: no longer exported. Use the
NEVERconstant instead. - Symbol.observable: RxJS will no longer be polyfilling Symbol.observable. That should be done by an actual polyfill library. This is to prevent duplication of code, and also to prevent having modules with side-effects in rxjs.
- bindCallback: removes result selector, use
mapinstead:bindCallback(fn1, fn2)()becomesbindCallback(fn1)().pipe(map(fn2)) - Symbol.iterator: We are no longer polyfilling Symbol.iterator. That would be done by a proper polyfilling library
- Observable.if: TypeScript users using
Observable.ifwill have to castObservableas any to get to if. It is a better idae to just useiifdirectly viaimport { iif } from 'rxjs'; - bindNodeCallback: resultSelector removed, use
mapinstead:bindNodeCallback(fn1, fn2)()becomesbindNodeCallback(fn1)().pipe(map(fn2)) - Rx.ts: importing from
rxjs/Rxis no longer available. Upcoming backwards compat solution will allow that - fromEvent: result selector removed, use
mapinstead:fromEvent(target, 'click', fn)becomesfromEvent(target, 'click').pipe(map(fn)) - last: no longer accepts
resultSelectorargument. To get this same functionality, usemap. - first: no longer supports
resultSelectorargument. The same functionality can be achieved by simply mapping either before or afterfirstdepending on your use case. - exhaustMap:
resultSelectorno longer supported, to get this functionality use:source.pipe(exhaustMap(x => of(x + x).pipe(map(y => x + y)))) - switchMap|switchMapTo:
switchMapandswitchMapTono longer takeresultSelectorarguments, to get the same functionality useswitchMapandmapin combination:source.pipe(switchMap(x => of(x + x).pipe(y => x + y))). - mergeMapTo:
mergeMapTono longer accepts a resultSelector, to get this functionality, you’ll want to usemergeMapandmaptogether:source.pipe(mergeMap(() => inner).pipe(map(y => x + y))) - fromEventPattern: no longer supports a result selector, use
mapinstead:fromEventPattern(fn1, fn2, fn3)becomesfromEventPattern(fn1, fn2).pipe(map(fn3))
重點整理
-
Import 位置簡化
-
creation functions 現在改由
rxjsimport1
import { of, from } from 'rxjs';
-
types and interfaces 現在改由
rxjsimport
-
-
語法調整
_if修改成iif_throw修改成throwErrorasap修改成asapScheduler等
-
擁有
resultSelector的operators都被拔掉了,建議改搭配使用map,例如1
source.pipe(mergeMap((x)=> of(x+x).pipe(y=> x=y)))
-
Pipeable operators 必須從
rxjs/operators引用 -
importing from
rxjs/Rx無法繼續使用. 但會提供rxjs-compat套件做向下相容