我已阅读this answer which was very detailed but one had thing caught me eyes:
import { MyThingService } from '../my-thing.service';
@Component({
selector: 'my-thing',
templateUrl: './my-thing.component.html'
})
export class MyThingComponent implements OnDestroy, OnInit {
private ngUnsubscribe: Subject = new Subject();
constructor(
private myThingService: MyThingService,
) { }
ngOnInit() {
this.myThingService.getThings()
.takeUntil(this.ngUnsubscribe)
.subscribe(things => console.log(things));
/* if using lettable operators in rxjs ^5.5.0
this.myThingService.getThings()
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(things => console.log(things));
*/
this.myThingService.getOtherThings()
.takeUntil(this.ngUnsubscribe)
.subscribe(things => console.log(things));
}
ngOnDestroy() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
}
创建主题以处理要完成的所有信号。 但是在ngOnDestroy中查看主题本身:为什么他们也完成了这个主题?垃圾收集时不会被收集吗?我可以同意这样的想法,即完成它是正确的,但是没有必要,因为gc将查看组件并看到该主题未被使用而不引用任何其他对象
问题
我在这里遗漏了什么吗?我们还必须打电话给完成吗?