Bird
0
0

What is wrong with this service code?

medium📝 Debug Q7 of 15
Angular - Services and Dependency Injection
What is wrong with this service code?
@Injectable({ providedIn: 'root' })
export class UserService {
  constructor() {}
  static providedIn = 'root';
}
ANo error; this code is valid.
BStatic property providedIn conflicts with decorator option.
CService cannot have static properties.
DConstructor must have parameters for injection.
Step-by-Step Solution
Solution:
  1. Step 1: Identify duplicate providedIn usage

    The decorator uses providedIn, but the class also declares a static providedIn property.
  2. Step 2: Understand conflict

    The static property shadows the decorator metadata, causing confusion or errors.
  3. Final Answer:

    Static property providedIn conflicts with decorator option. -> Option B
  4. Quick Check:

    Do not declare static providedIn in service class [OK]
Quick Trick: Avoid static providedIn property in service classes [OK]
Common Mistakes:
MISTAKES
  • Adding static providedIn property
  • Assuming constructor must have parameters
  • Thinking static properties are forbidden

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes