Bird
0
0

Which is the correct way to inject a service named DataService into an Angular standalone component using the new inject() function?

easy📝 Syntax Q12 of 15
Angular - Services and Dependency Injection
Which is the correct way to inject a service named DataService into an Angular standalone component using the new inject() function?
Aconst dataService = inject(DataService); inside the component class body
Bconstructor(private dataService: DataService) {} inside the component class
Cimport { DataService } from './data.service'; outside the component
D@Inject(DataService) dataService; inside the component class
Step-by-Step Solution
Solution:
  1. Step 1: Recall the new inject() function usage

    In Angular 17+, inject() is called inside the component class body to get the service instance.
  2. Step 2: Identify the correct syntax

    Using const dataService = inject(DataService); inside the class body is the correct pattern.
  3. Final Answer:

    const dataService = inject(DataService); inside the component class body -> Option A
  4. Quick Check:

    inject() function usage = const dataService = inject(DataService); inside the component class body [OK]
Quick Trick: Use inject() inside class body for standalone components [OK]
Common Mistakes:
MISTAKES
  • Using constructor injection instead of inject() in standalone components
  • Trying to use @Inject decorator incorrectly
  • Confusing import statements with injection

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes