Angular · Junior · Question 30 of 75
How do you get a service instance inside a component?
- ✓Correct answer:
private cart = inject(CartService);(or a constructor parameter) - 2
private cart = new CartService(); - 3
import cart from './cart.service'gives the instance automatically
Explanation
Dependency injection supplies the instance, so all components share one object and tests can replace it with a fake. new creates an unrelated copy and hard-wires the dependency.