Angular 6 將會把 RxJS 一起升級到 6 版,在 RxJS 6 版有許多的 breaking changes,這些 breaking changes 有許多是為了簡化開發時的寫法所做的改變,所以不要太緊張,就慢慢的了解就可以了。
RxJS 6 Breaking Changes 列表
- webSocket:
webSocket
creator function now exported fromrxjs/websocket
aswebsocket
. - IteratorObservable: IteratorObservable no longer share iterator between subscription
- utils: Many internal use utilities like
isArray
are now hidden underrxjs/internal
, they are implementation details and should not be used. - testing observables:
HotObservable
andColdObservable
, and other testing support types are no longer exported directly. - creation functions: All create functions such as of, from,
combineLatest
andfromEvent
should now be imported fromrxjs/create
. - types and interfaces: Can no longer explicitly import types from
rxjs/interfaces
, import them fromrxjs
instead - symbols: Symbols are no longer exported directly from modules such as
rxjs/symbol/observable
please useSymbol.observable
andSymbol.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 rxjs
like 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
ArrayObservable
orForkJoinObservable
. - _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
distinct
requires aSet
implementation 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
, andrace
have been removed. Please use the static versions of those operations. e.g.a.pipe(concat(b, c)) becomes concat(a, b, c)
. - rxjs:
rxjs/create
items 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
NEVER
constant 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
map
instead: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.if
will have to castObservable
as any to get to if. It is a better idae to just useiif
directly viaimport { iif } from 'rxjs';
- bindNodeCallback: resultSelector removed, use
map
instead:bindNodeCallback(fn1, fn2)()
becomesbindNodeCallback(fn1)().pipe(map(fn2))
- Rx.ts: importing from
rxjs/Rx
is no longer available. Upcoming backwards compat solution will allow that - fromEvent: result selector removed, use
map
instead:fromEvent(target, 'click', fn)
becomesfromEvent(target, 'click').pipe(map(fn))
- last: no longer accepts
resultSelector
argument. To get this same functionality, usemap
. - first: no longer supports
resultSelector
argument. The same functionality can be achieved by simply mapping either before or afterfirst
depending on your use case. - exhaustMap:
resultSelector
no longer supported, to get this functionality use:source.pipe(exhaustMap(x => of(x + x).pipe(map(y => x + y))))
- switchMap|switchMapTo:
switchMap
andswitchMapTo
no longer takeresultSelector
arguments, to get the same functionality useswitchMap
andmap
in combination:source.pipe(switchMap(x => of(x + x).pipe(y => x + y)))
. - mergeMapTo:
mergeMapTo
no longer accepts a resultSelector, to get this functionality, you’ll want to usemergeMap
andmap
together:source.pipe(mergeMap(() => inner).pipe(map(y => x + y)))
- fromEventPattern: no longer supports a result selector, use
map
instead:fromEventPattern(fn1, fn2, fn3)
becomesfromEventPattern(fn1, fn2).pipe(map(fn3))
重點整理
-
Import 位置簡化
-
creation functions 現在改由
rxjs
import1
import { of, from } from 'rxjs';
-
types and interfaces 現在改由
rxjs
import
-
-
語法調整
_if
修改成iif
_throw
修改成throwError
asap
修改成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
套件做向下相容