Bird
0
0

Identify the error in this Angular code using TransferState:

medium📝 Debug Q14 of 15
Angular - Server-Side Rendering
Identify the error in this Angular code using TransferState:
const DATA_KEY = makeStateKey('data');
this.transferState.set(DATA_KEY, { value: 42 });
const data = this.transferState.get('DATA_KEY', null);
AThe key passed to get() should be the StateKey object, not a string
BThe set() method cannot store objects, only strings
CmakeStateKey should not be used for keys
DtransferState cannot be used outside ngOnInit
Step-by-Step Solution
Solution:
  1. Step 1: Check key usage in get()

    The get method requires the same StateKey object used in set, not a string.
  2. Step 2: Validate other statements

    Objects can be stored, makeStateKey is correct, and transferState can be used anytime in component lifecycle.
  3. Final Answer:

    The key passed to get() should be the StateKey object, not a string -> Option A
  4. Quick Check:

    Use same StateKey object for set and get [OK]
Quick Trick: Pass StateKey object, not string, to get() [OK]
Common Mistakes:
  • Passing string instead of StateKey to get()
  • Thinking set() only accepts strings
  • Misunderstanding when transferState can be used

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes