Typescript rxjs Observable 数组连接 [英] Typescript rxjs Observable array concat
问题描述
我在使用带有打字稿的 rxjs Observable.concat 函数时遇到问题.收到错误无法读取未定义的属性‘应用’"
I am having trouble using rxjs Observable.concat function with typescript. Getting an error "Cannot read property 'apply' of undefined"
问题似乎只出现在打字稿和可能特定于 rxjs 版本 5 concat.该代码似乎适用于 rxjs V4.
The problem seems to be only within typescript and may be specific to rxjs version 5 concat. The code seems to work with rxjs V4.
这是说明问题的代码的简化版本...
Here is a simplified version of the code illustrating the problem...
/*jshint esnext: true */
console.clear();
console.log('started');
class test{
observableArray: Observable<any>[]=[];
constructor(){
this.observableArray.push(Rx.Observable.return("Line 1"));
this.observableArray.push(Rx.Observable.return(56));
this.observableArray.push(Rx.Observable.create((observer)=>{
setTimeout(()=>{
try{
observer.onNext("Waited for");
observer.onCompleted();
}
catch(err){
observer.onError(err);
}
},3000);
}));
}
run(){
// ... indeterminate number of observables pushed into array.
// The problem lies below
var source = Rx.Observable.concat(...this.observableArray);
// seems to transpile into js: source = Observable_1.Observable.concat.apply(Observable_1.Observable, this.observableArray);
// In the chrome debugger I am getting error: Cannot read property 'apply' of undefined
var subscription = source.subscribe(
function (x) {
console.log('Next: ' + x);
},
function (err) {
console.error('Error: ' + err);
},
function () {
console.log('Completed');
});
}
}
}
这是jsbin:https://jsbin.com/naxeba/edit?html,js,console,output
推荐答案
好的,问题解决了.
Reactive js 版本 5 用户的重要说明:在使用 rxjs 的打字稿中,为了最小化应用程序的大小,必须专门为要包含的函数/运算符导入每个运算符.所以这条线...
Important note for users of reactive js version 5: In typescript with rxjs, to minimize app size each operator must be specifically imported for the function/operator to be included. So the line...
import {concat} from 'rxjs/operators/concat'
必须包含在打字稿文件的顶部才能使 concat 工作.
import {concat} from 'rxjs/operators/concat'
must be inlcuded at the top of the typescript file for concat to work.
令我困惑的是,我在 VS2015 中获得了 Observable.concat 函数的智能感知,即使该函数实际上并未被导入.
What confused me was that I was getting intellisense in VS2015 for the Observable.concat function even though the function had not actually been imported.
这篇关于Typescript rxjs Observable 数组连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!