【问题标题】:error TS2339: Property 'combineLatest' does not exist on type 'typeof Observable'错误 TS2339:“typeof Observable”类型上不存在属性“combineLatest”
【发布时间】:2018-08-21 14:41:52
【问题描述】:

我在尝试启动 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 中

【问题讨论】:

    标签: angular rxjs6


    【解决方案1】:

    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

    其中包含更多示例

    【讨论】:

    • 我尝试了您的解决方案并编辑了我的第一篇文章,因为我有另一个错误(关于 ts-node)
    • 终于找到我的问题了。当我启动 ts-node 命令时,我在 package.json 中添加了这个配置: "config": "ts-node -O '{\"module\": \"commonjs\"}' git.version.ts",
    • @JoeAllen 很高兴你找到它,只是在那里为你输入可能的内容;)
    • 你的解决方案比我的更干净 ;)
    【解决方案2】:

    我也遇到了同样的问题

    替换 import {Observable} from 'rxjs/Observable'

    by: 从 'rxjs/Rx' 导入 {Observable}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-29
      • 2018-10-25
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多