12

How can I export a service from a module in angular 2?

The idea is that when I import into another component, I want the import to be agnostic of the actual service location, it should be the repsonsibility of the module to manage that

Core.module.ts:

import {
NgModule,
Optional, SkipSelf } from '@angular/core';
import { CommonModule } from '@angular/common';

import { MyApi } from './Api/myApi.service';

import { AuthenticationModule } from './authentication/authentication.module';

@NgModule({
imports: [
    CommonModule,
    AuthenticationModule
],
providers: [
    MyApi
],
exports: [MyApi, AuthenticationModule]
})
export class CoreModule {
constructor( @Optional() @SkipSelf() parentModule: CoreModule) {
    if (parentModule) {
    throw new Error(
        'CoreModule is already loaded. Import it in the AppModule only');
    }
}

}

App.component.ts:

import { Router, ActivatedRoute } from '@angular/router';
import { Component, ViewContainerRef, OnInit } from '@angular/core';

import { MyApi } from 'core.module'; //i want this to be the core module import, not './core/api/myApi.service'

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
    constructor ( public service: MyApi) {
        service.doStuff()
    }
}

But in the code example above, it tells me that MyApi isn't exported by Core.module.

This is slightly pseudo-code, so excuse the small errors I've made :)

2
  • Are you trying to implement barrel behavior? I mean, you have a directory with all your related module stuff, so you can import anything from that specific module/directory with import { whatever } from 'path/to/module. Cause there's a difference between the @NgModule exports property and a static typescript/javascript module export statement.
    – Osman Cea
    Dec 20, 2017 at 12:37
  • Yeah I am. @OsmanCea. I was advised at some point that ngModules were meant to be a replacement for rolling up all the files in a directory with barrels, trying to figure out if I can.
    – Alex
    Dec 20, 2017 at 12:42

2 Answers 2

7

There's two things you can do to make your imports more concise:

  1. You can export everything from your entry point file (by convention is often called index.ts) and later import any class you've exported from that file:

    import { NgModule } from '@angular/core';
    import { ApiService } from 'path/to/api.service';
    @NgModule({ ... })
    export class CoreModule {}
    export * from 'path/to/api.service';
    

    This way you can then import both CoreModule and ApiService from the same route like this:

    import { CoreModule, ApiService } from 'path/to/core.module;'
    

    So you have a common entry point for all your module dependencies.

  2. If your module is deeply nested, or you want to import it from a location that might end up in going back and forth a few directories, you can always create an alias for that path in your main tsconfig.json file, under compilerOptions.paths:

    {
      "compilerOptions": {
        // ...
        "paths": {
          "@app-core": ["app/path/to/deeply/nested/core.module"]
        }
      }
    }
    

    And then use that alias instead:

    import { CoreModule, ApiService } from '@app-core'
    
1

You have to add this line into your core.module

export {MyApi} from './Api/myApi.service';

2
  • I think this is the valid approach, how is the NgModule (which executes at runtime) supposed to handle static imports? I don't think that something you can do
    – Osman Cea
    Dec 20, 2017 at 12:56
  • ES module export and angular export are two different things. What I propose is just ES module export which means you just export the class itself. There is nothing angular related going on in this line. Also, OP should not put MyApi in exports array btw. Angular modules do not exports services, just directives/pipes/modules. Dec 20, 2017 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.