Bird
0
0

Identify the issue in this Angular service injection:

medium📝 Debug Q6 of 15
Angular - Services and Dependency Injection
Identify the issue in this Angular service injection:
@Injectable()
export class UserService {}
@Component({selector: 'app-main', template: ''})
export class MainComponent {
  constructor(private userService) {}
}
AUserService must be provided in the component's providers array
BUserService is missing the @Component decorator
CMainComponent should not inject services in the constructor
DThe constructor parameter lacks a type annotation for UserService
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor parameter

    The constructor parameter 'userService' lacks a type, so Angular cannot inject the service.
  2. Step 2: Other options invalid

    UserService is a service, not a component; injecting in constructor is correct; providedIn or providers array is optional if provided elsewhere.
  3. Final Answer:

    The constructor parameter lacks a type annotation for UserService -> Option D
  4. Quick Check:

    Constructor params must be typed for DI [OK]
Quick Trick: Always type constructor parameters for DI [OK]
Common Mistakes:
MISTAKES
  • Omitting type annotation in constructor
  • Confusing service with component decorators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes