我正在尝试在Angular 2中使用observable来提供服务。
但是我收到了这个错误:
Uncaught TypeError: Cannot read property 'isStopped' of undefined
偷看我的服务:
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
@Injectable()
export class Service{
getList(){
return new Observable((observer)=>{
observer.next(result);
})
}
}
实施:
import ...
@Component({...})
export class List implements OnInit {
list : any[];
list$ : Observable<Array<any>>;
constructor(...){
}
ngOnInit(){
this.list$ = this.Service.getList();
this.list$.subscribe(
(items) => {
this.list = items;
console.log("triggered");
},
(error)=>{
console.error(error);
},
()=>{
console.log("completed");
}
);
}
}
有没有人有这个错误?我找不到任何相关内容。
=============================================== =================
编辑:
很抱歉这是“isStopped”的来源: https://github.com/ReactiveX/rxjs/blob/master/src/Subscriber.ts#L94
来自rxjs库。
答案 0 :(得分:1)
似乎我在某个点使用回调作为observable中的参数并删除它修复了问题。我仍然不明白为什么,但我猜它必须对Observables的工作方式做些什么。
答案 1 :(得分:0)
为了更加清楚地说明这个问题,我提供了a JSFiddle来演示和解释该问题。
以下内容将引发错误:
未捕获的TypeError:无法读取未定义的属性'isStopped'。
return FutureBuilder(
future: UserController.getActivityByDate(
{"date": widget.index.toIso8601String(), "id": widget.user}),
builder: (context, snapshot) {
if (snapshot.hasData != null) {
print(snapshot.data);
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, position) {
var item = snapshot.data.elementAt(position);
return Container(
child: Card(
child: ListTile(
title: Text("${item["activity"]["project"]}"),
subtitle: Text(item["created_at"]),
),
),
);
});
}
},
);
要保留订户对象的somePromise
.then(result => {
subscriber.next(result);
subscriber.complete();
})
.catch(subscriber.error);
方法所期望的适当范围,必须创建一个匿名函数:
subscriber.error
此JS小提琴的JS部分演示了一个打字稿示例。