angular - 错误 TS2339 : Property 'combineLatest' does not exist on type 'typeof Observable'

标签 angular rxjs6

当我尝试启动 angular 6 项目时遇到问题。我有这个错误:

"TSError: ⨯ 无法编译 TypeScript:
git.version.ts(34,29): 错误 TS2339: 'typeof Observable' 类型上不存在属性 'combineLatest'。"

它与 Angular 5 配合得很好,但我更新了 RxJs(从 5 到 6),现在它不起作用了。

import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { Observable, combineLatest } from 'rxjs';


let exec = require('child_process').exec;

let tag = new Observable<string>(s => {
    exec('git describe --tags $(git rev-list --tags --max-count=1)',
        function (error: Error, stdout: Buffer, stderr: Buffer) {
            if (error !== null) {
                console.log('git error: ' + error + stderr);
            }
            s.next(stdout.toString().trim());
            s.complete();
        });
});



let revision = new Observable<string>(s => {
    exec('git rev-parse --short HEAD',
        function (error: Error, stdout: Buffer, stderr: Buffer) {
            if (error !== null) {
                console.log('git error: ' + error + stderr);
            }
            s.next(stdout.toString().trim());
            s.complete();
        });
});

Observable.combineLatest(tag, revision).subscribe(([tag, revision]) => {
        console.log(`version: '${tag}', revision: '${revision}'`);

        const content = '// this file is automatically generated\n' +
            `export const version = {version: '${tag}', revision: '${revision}'};`;

        writeFileSync(
            'src/environments/version.ts',
            content,
            {encoding: 'utf8'}
        );
    });

我启动的命令:“ts-node git.version.ts”是获取提交和我使用的标签版本。

非常感谢 !

更新 :
combineLatest(tag, revision).subscribe(([tag, revision]) => {
        console.log(`version: '${tag}', revision: '${revision}'`);

        const content = '// this file is automatically generated\n' +
            `export const version = {version: '${tag}', revision: '${revision}'};`;

        writeFileSync(
            'src/environments/version.ts',
            content,
            {encoding: 'utf8'}
        );
    });

我没有任何关于 CombineLatest 的错误,但我有来自 ts-node 的错误:
function (exports, require, module, __filename, __dirname) { import { writeFileSync } from 'fs';
                                                              ^^^^^^

SyntaxError: Unexpected token import

任何的想法 ?

更新 2:要修复此错误:
"config": "ts-node -O '{\"module\": \"commonjs\"}' git.version.ts",

在 package.json 中

最佳答案

Angular6 带有 rxjs6。这是 combineLatest 的正确格式:

从新的“rxjs”位置在类声明之前导入:

// RxJS v6+
import { timer, combineLatest } from 'rxjs';

函数内部使用示例:
//timerOne emits first value at 1s, then once every 4s
const timerOne = timer(1000, 4000);
//timerTwo emits first value at 2s, then once every 4s
const timerTwo = timer(2000, 4000);
//timerThree emits first value at 3s, then once every 4s
const timerThree = timer(3000, 4000);

//when one timer emits, emit the latest values from each timer as an array
const combined = combineLatest(timerOne, timerTwo, timerThree);

const subscribe = combined.subscribe(
  ([timerValOne, timerValTwo, timerValThree]) => {
    /*
      Example:
    timerOne first tick: 'Timer One Latest: 1, Timer Two Latest:0, Timer Three Latest: 0
    timerTwo first tick: 'Timer One Latest: 1, Timer Two Latest:1, Timer Three Latest: 0
    timerThree first tick: 'Timer One Latest: 1, Timer Two Latest:1, Timer Three Latest: 1
  */
    console.log(
      `Timer One Latest: ${timerValOne},
     Timer Two Latest: ${timerValTwo},
     Timer Three Latest: ${timerValThree}`
    );
  }
);

当然,在您的情况下,您可以用 tag, revision 替换计时器。项目。

代码摘自:

https://www.learnrxjs.io/operators/combination/combinelatest.html

其中包含更多示例

关于angular - 错误 TS2339 : Property 'combineLatest' does not exist on type 'typeof Observable' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51951200/

相关文章:

angular - 在 RxJs Observable 中包装一个 API 函数

angular - 如何通过 ngrx 存储中的单个操作更新多个 reducer 的状态?

unit-testing - 如何刷新一个冷的 observable 而不是测试中的所有 observable?

javascript - 将 AngularJS 升级为 Angular,AppModule 被引导,但它没有声明 "@NgModule.bootstrap"错误

html - border-radius-top-left 不适用于 Angular2

css - Angular Material 将垫子墨水条旋转到垂直位置

angular - 错误 : Can't resolve 'rxjs/add/operator/map'

typescript - RxJS6:为什么可观察的管道运算符只接收 OperatorFunction 而不是 MonoTypeOperatorFunction

angular - 正确使用 rxjs-5-to-6-migrate 。 tsconfig 路径的问题

angular - RxJS 可观察对象 : why does callback fire twice?