admin管理员组文章数量:1302431
I am having trouble using rxjs Observable.concat function with typescript. Getting an error "Cannot read property 'apply' of undefined"
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');
});
}
}
}
Here is the jsbin: ,js,console,output
I am having trouble using rxjs Observable.concat function with typescript. Getting an error "Cannot read property 'apply' of undefined"
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');
});
}
}
}
Here is the jsbin: https://jsbin./naxeba/edit?html,js,console,output
Share Improve this question edited Jul 15, 2018 at 5:28 Aluan Haddad 31.9k10 gold badges83 silver badges95 bronze badges asked Apr 12, 2016 at 22:50 JeffCJeffC 2931 gold badge3 silver badges9 bronze badges2 Answers
Reset to default 6OK, Problem solved.
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'
must be inlcuded at the top of the typescript file for concat to work.
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.
I'm using rxjs 5.5.7 and had to use import 'rxjs/add/observable/concat';
.
The code itself looks like:
Observable.concat(...observables).subscribe etc
本文标签: javascriptTypescript rxjs Observable array concatStack Overflow
版权声明:本文标题:javascript - Typescript rxjs Observable array concat - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741669564a2391519.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论