错误TS2339:类型“ typeof Observable”上不存在属性“ combineLatest”

时间:2018-08-21 14:41:52

标签: angular rxjs6

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

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

它在Angular 5上运行良好,但是我将RxJ更新(从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中

2 个答案:

答案 0 :(得分:3)

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

其中包含更多示例

答案 1 :(得分:-1)

我遇到了同样的问题

从“ rxjs / Observable”替换了导入{Observable}

作者:从'rxjs / Rx'导入{Observable}