两者之间有什么区别吗?
Observable.pipe(take(1)).subscribe(...)vs
const subscription = Observable.subscribe(() => {
// Do something, then
subscription.unsubscribe()
})发布于 2016-10-28 14:48:10
与subscribe相比,take(1)方法有许多优点
take(4)将保持简单,而第二种方法将变得难以编码。第三项是与rxjs相关的,其他的与编码风格有关。
看一看sample here。
发布于 2016-12-02 05:52:25
在Angular2中,我发现自己同时使用了这两种范式。
第一个在方法内部最有意义,而第二个更适合在构造函数中使用,并在解构函数中进行清理。
doThing(){
this.store.select('thing').pipe(take(1))
.subscribe(item => {
otherMethod(item)
});
}vs
class SomeClass{
public val;
private sub;
constructor(){
this.sub = this.store.select('thing')
.subscribe(item => {
this.val = item
});
}
ngDestroy() {
this.sub.unsubscribe()
}
}https://stackoverflow.com/questions/40297826
复制相似问题