Bird
0
0

You have this Angular component:

medium📝 Debug Q14 of 15
Angular - Lifecycle Hooks
You have this Angular component:
export class DataComponent implements OnDestroy {
  subscription = this.dataService.getData().subscribe();

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

What is the problem with this code?
AThe unsubscribe method is not called correctly
BThe subscription is never created
CngOnDestroy is missing the return type
DThere is no problem; code works fine
Step-by-Step Solution
Solution:
  1. Step 1: Check how unsubscribe is used

    The code uses this.subscription.unsubscribe; without parentheses, so the method is not actually called.
  2. Step 2: Correct usage of unsubscribe

    It should be this.subscription.unsubscribe(); to properly stop the subscription and avoid memory leaks.
  3. Final Answer:

    The unsubscribe method is not called correctly -> Option A
  4. Quick Check:

    Call unsubscribe() with parentheses [OK]
Quick Trick: Always call unsubscribe() as a function [OK]
Common Mistakes:
  • Forgetting parentheses on unsubscribe
  • Assuming unsubscribe is a property
  • Ignoring ngOnDestroy lifecycle

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes