Observable.zip不是一个函数

时间:2017-05-16 13:29:49

标签: angular typescript rxjs typescript2.1

  

VM95422:27原始例外:    WEBPACK_IMPORTED_MODULE_3_rxjs_Observable .Observable.zip不是函数

尝试各种进口

// import 'rxjs/add/operator/zip';
// import 'rxjs/add/observable/zip-static';
// import 'rxjs/add/operator/zip';
import 'rxjs/operator/zip';

尝试使用它:

const zippedUsers: Observable<User[]> = Observable.zip<User>(this.usersObservable);

Angular 4,TypeScript 2.1.6

的package.json:

"rxjs": "^5.1.0",

3 个答案:

答案 0 :(得分:24)

可能像

import {Observable} from "rxjs/Observable";
import "rxjs/add/observable/zip";

然后像:

Observable.zip(this.someProvider.getA(), this.someProvider.getB())
        .subscribe(([a, b]) => {
            console.log(a);
            console.log(b);
        });

答案 1 :(得分:8)

RxJS 6

从RxJS 6开始...

Observable creation functions

例如from()fromPromise()of()zip()应该这样导入:

import { from, fromPromise, of, zip } from 'rxjs';

并用作普通函数调用:

const data: Observable<any> = fromPromise(fetch('/api/endpoint'));

Pipeable operators

应这样导入:

import { map, filter, scan } from 'rxjs/operators';

并用作pipe()方法参数:

const someObservable: Observable<number> = ...;
const squareOddVals = someObservable.pipe(
        filter((n: number) => n % 2 !== 0),
        map(n => n * n))
    .subscribe((n: number): void => ...);

答案 2 :(得分:4)

5.5 rxjs:

import {zip} from "rxjs/observable/zip";
const zippedUsers: Observable<User[]> = zip(this.usersObservable);