1
votes

Tech: using angular 6, angular cli and typescript.

Scenario:

I have an app module and a core module

I want my app modules components to use the services from my core module.

My core module contains a list of services:

import { NgModule } from '@angular/core';

// Services
import { AuthService } from './services/auth.service';

@NgModule({
  declarations: [
  ],
  imports: [
  ],
  providers: [
    AuthService
  ]
})
export class CoreModule { }

This is my app module:

import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';

import { AppComponent } from './app.component';

// Modules
import { CoreModule } from './core/core.module';

// Sections
import { COMPONENTS } from './components/index';
import { ROUTES } from './app.routes';

@NgModule({
  declarations: [
    AppComponent,
    COMPONENTS
  imports: [
    CommonModule,
    FormsModule,
    BrowserModule,
    HttpClientModule,
    ReactiveFormsModule,
    RouterModule.forRoot(ROUTES),
    CoreModule
  ],
  providers: [
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

Note: As you can see, I'm importing the CoreModule.

This is my component from the app module (its calling the core module to get the AuthService):

import { Component } from '@angular/core';

// This Auth Service import not working (says service not exported)
import { AuthService } from '../../core/core.module';

@Component({
  selector: 'app-contact',
  templateUrl: './contact.component.html'
})
export class ContactComponent {

  constructor(
    private authService: AuthService
  ) {}
}

Issue: I'm trying to import a service into my app modules component called contact component like so:

import { AuthService } from '../../core/core.module';

Error I get:

Service not an exported member of core module.

Outcome: I want my contact component from app module to import the core module service called AuthService.

2
What does your service look like? (Just the declaration)DeborahK
Can you repo this is a small test stackblitz?DeborahK

2 Answers

3
votes

Since you'd imported your CoreModule in AppModule where you'd also decalred your ContactComponent (seems in COMPONENTS), both service and component belong to the same Module here, so they can see each other. What'll have to do in your ContactComponent is merely import the service AuthService from its real location (not ../../core/core.module ).

import { AuthService } from ' <...>/auth.service';
0
votes

If the module is properly imported in the root module, I think that if you add "export ... from ..." to your mymodule.module.ts:

import { MystuffService } from file 'mystuff.service'. 
export { MystuffService } from file 'mystuff.service'.

Then you can import your service:

import { MystuffService } from 'mymodule.module'