gpt4 book ai didi

Angular 10 : Should I unsubscribe form subscription to valueChanges Observable in ngOnDestroy?

转载 作者:行者123 更新时间:2023-12-05 08:04:49 42 4
gpt4 key购买 nike

我有这样的表格:

this.form = this.fb.group({
username: ["Put name"],
address: ["Put address"]
});

我正在听用户名输入值:

this.form.controls.username.valueChanges.subscribe(val =>
//doSomething()
);

你认为我应该添加一个 pipe(takeUntil(this.destroy$)) 以避免内存泄漏吗?

有没有办法测试当组件被销毁时我是否不取消订阅,Observable 仍然存在于内存中?

最佳答案

除了销毁,你也可以这样做

public sub: Subscription;

contructor() {
this.sub = new Subscription();
}

this.sub.add(this.form.controls.username.valueChanges.subscribe(val =>
//doSomething()
));

ngOnDestroy() {
this.sub.unsubscribe();
}

关于 Angular 10 : Should I unsubscribe form subscription to valueChanges Observable in ngOnDestroy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67315222/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com