Bird
0
0

You want to create a shared Angular module that provides a service and a component to other modules. Which of the following is the correct way to configure SharedModule?

hard📝 Application Q8 of 15
Angular - Modules
You want to create a shared Angular module that provides a service and a component to other modules. Which of the following is the correct way to configure SharedModule?
A@NgModule({ declarations: [SharedComponent], exports: [SharedComponent], providers: [SharedService] }) export class SharedModule {}
B@NgModule({ declarations: [SharedComponent], providers: [SharedService] }) export class SharedModule {}
C@NgModule({ exports: [SharedComponent], providers: [SharedService] }) export class SharedModule {}
D@NgModule({ declarations: [SharedComponent], exports: [SharedComponent] }) export class SharedModule {}
Step-by-Step Solution
Solution:
  1. Step 1: Include component in declarations and exports

    To share a component, declare it and export it so other modules can use it.
  2. Step 2: Provide the service in providers

    Services are added to providers to be injectable in modules importing this shared module.
  3. Final Answer:

    @NgModule({ declarations: [SharedComponent], exports: [SharedComponent], providers: [SharedService] }) export class SharedModule {} -> Option A
  4. Quick Check:

    Declare and export components; provide services [OK]
Quick Trick: Declare and export components; provide services in providers [OK]
Common Mistakes:
  • Forgetting to export the component
  • Not providing the service
  • Trying to export services

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes