Angular 2 ERROR in Cannot define module for class ManagersService i

When trying to build with ng build -prod I get the following error

ERROR in Cannot determine the module for class ManagersService in C:/Test/src/app/shared/common/managers/managers.service.ts! Add ManagersService to the NgModule to fix it.

      

I have AppModule, AppCommonModule and CustomModuleA. ManagersService is located in AppCommonModule. CustomModuleA has a route that uses ManagersComponent (this component uses ManagersService) AppCommonModule. AppCommonModule is imported in both AppModule and CustomModuleA

I added a service to the AppCommonModule providers. I don't understand what I am missing here.

Note. With an ng service, this works like a charm. Mistake.

This is the ManagersComponent that belongs to the AppCommonModule

@Component({
    templateUrl: './managers.component.html',
    providers: [ManagersService]
})
export class ManagersComponent extends AppComponentBase {
    @ViewChild('createOrEditManagerModal') createOrEditManagerModal: CreateOrEditManagerModalComponent;

    constructor(public managersService: ManagersService) {
    }

      

This is the AppCommonModule

@NgModule({
    imports: [
        FormsModule,
        ReactiveFormsModule        
    ],
    declarations: [
        ManagersComponent        
    ],
    providers: [
        ManagersService,
    ],
    exports: [      
        ManagersComponent,
    ]
})

      

and this is CustomModuleA

@NgModule({   
    imports: [
        CommonModule,
        FormsModule,
        HttpModule,
        ModalModule,
        TabsModule,
        TooltipModule,
        AppCommonModule,        
    ],
     declarations: [
        CompA     
    ],
    providers: [
       SomeOtherService
    ]
})

export class CustomModuleA { }

      

This is CustomModuleRouting

NgModule({
    imports: [
        RouterModule.forChild([
            {
                path: '',
                children: [                    
                    {
                        path: 'compA',
                        component: CompA,                       
                    },                    
                    {
                        path: 'managers',
                        component: ManagersComponent,                        
                    },
                ]
            }
        ])
    ],
    exports: [
        RouterModule
    ]
})

      

+3


source to share


1 answer


I don't see your import in the AppCommonModule, but I am assuming you have two files containing ManagersService and only import one of them (not src / app / shared / common / managers / manager.service.ts). Removing the corrupting spoof file will help solve your problem.



The --prod switch starts AOT compilation, which will try to compile every .ts file it finds. ng serve uses JIT compilation, so the problem doesn't show up there.

+4


source







All Articles