[英]Typescript rxjs Observable array concat
我在使用带脚本的rxjs Observable.concat函数时遇到问题。 收到错误“无法读取属性'应用'未定义”
问题似乎只在打字稿中,可能特定于rxjs版本5 concat。 该代码似乎适用于rxjs V4。
以下是说明问题的代码的简化版本......
/*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
好的,问题解决了。
对于反应性js版本5的用户的重要说明:在带有rxjs的打字稿中,为了最小化应用程序大小,必须为要包含的函数/运算符专门导入每个运算符。 这条线......
必须在import {concat} from 'rxjs/operators/concat'
文件的顶部包含import {concat} from 'rxjs/operators/concat'
以使concat工作。
令我感到困惑的是,我在VS2015中获得了Observable.concat函数的intellisense,即使该函数实际上没有被导入。
我正在使用rxjs 5.5.7并且必须使用import 'rxjs/add/observable/concat';
。
代码本身看起来像: Observable.concat(...observables).subscribe etc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.