Bird
0
0

Identify the issue in this Angular component's unsubscription code:

medium📝 Debug Q6 of 15
Angular - RxJS and Observables Fundamentals
Identify the issue in this Angular component's unsubscription code:
export class TestComponent implements OnDestroy {
  private subscription = this.service.getData().subscribe();

  ngOnDestroy() {
    this.subscription.unsubscribe;
  }
}
AThe unsubscribe method is referenced but not called, so unsubscription does not occur
BThe subscription variable is not declared properly
CngOnDestroy is not the correct lifecycle hook for unsubscription
DThe subscription should be assigned inside ngOnDestroy
Step-by-Step Solution
Solution:
  1. Step 1: Check unsubscribe syntax

    Unsubscribe is a method and must be invoked with parentheses.
  2. Step 2: Analyze given code

    Code uses this.subscription.unsubscribe; without parentheses, so method is not called.
  3. Step 3: Effect of missing parentheses

    Subscription remains active, causing potential memory leaks.
  4. Final Answer:

    The unsubscribe method is referenced but not called, so unsubscription does not occur -> Option A
  5. Quick Check:

    Always call unsubscribe() as a function [OK]
Quick Trick: Call unsubscribe() with parentheses [OK]
Common Mistakes:
MISTAKES
  • Forgetting parentheses when calling unsubscribe
  • Unsubscribing outside ngOnDestroy

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes