问题
VM95422:27 ORIGINAL EXCEPTION: WEBPACK_IMPORTED_MODULE_3_rxjs_Observable.Observable.zip is not a function
Tried various imports
// import 'rxjs/add/operator/zip';
// import 'rxjs/add/observable/zip-static';
// import 'rxjs/add/operator/zip';
import 'rxjs/operator/zip';
Trying to use it like that:
const zippedUsers: Observable<User[]> = Observable.zip<User>(this.usersObservable);
Angular 4, TypeScript 2.1.6
package.json:
"rxjs": "^5.1.0",
回答1:
maybe something like
import {Observable} from "rxjs/Observable";
import "rxjs/add/observable/zip";
then something like:
Observable.zip(this.someProvider.getA(), this.someProvider.getB())
.subscribe(([a, b]) => {
console.log(a);
console.log(b);
});
回答2:
RxJS 6
Starting from RxJS 6...
Observable creation functions
such as from()
, fromPromise()
, of()
, zip()
should be imported like this:
import { from, fromPromise, of, zip } from 'rxjs';
and used as a plain function call:
const data: Observable<any> = fromPromise(fetch('/api/endpoint'));
Pipeable operators
should be imported like this:
import { map, filter, scan } from 'rxjs/operators';
and used as pipe()
method arguments:
const someObservable: Observable<number> = ...;
const squareOddVals = someObservable.pipe(
filter((n: number) => n % 2 !== 0),
map(n => n * n))
.subscribe((n: number): void => ...);
回答3:
5.5 rxjs:
import {zip} from "rxjs/observable/zip";
const zippedUsers: Observable<User[]> = zip(this.usersObservable);
来源:https://stackoverflow.com/questions/44002910/observable-zip-is-not-a-function