错误 TS2339:属性 'combineLatest' 在类型 'typeof Observable' 上不存在
error TS2339: Property 'combineLatest' does not exist on type 'typeof Observable'
我在尝试启动 angular 6 项目时遇到问题。我遇到了这个错误:
"TSError: ⨯ 无法编译 TypeScript:
git.version.ts(34,29):错误 TS2339:属性 'combineLatest' 在类型 'typeof Observable' 上不存在。“
它在 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 的正确格式:
在 class 声明之前从新的 '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
其中包含更多示例
我遇到了同样的问题
替换了从 'rxjs/Observable'
导入 {Observable}
通过:从 'rxjs/Rx'
导入 {Observable}
我在尝试启动 angular 6 项目时遇到问题。我遇到了这个错误:
"TSError: ⨯ 无法编译 TypeScript: git.version.ts(34,29):错误 TS2339:属性 'combineLatest' 在类型 'typeof Observable' 上不存在。“
它在 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 的正确格式:
在 class 声明之前从新的 '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
其中包含更多示例
我遇到了同样的问题
替换了从 'rxjs/Observable'
导入 {Observable}通过:从 'rxjs/Rx'
导入 {Observable}