gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:43:16 26 4
gpt4 key购买 nike

当我尝试启动 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com