user7087866
user7087866

Reputation:

How to import a service from another module using angular2?

I'm trying to get the user from my LoginService to profile module. My profile module have 3 components. Loginservice is in another component inside the AppModule. This is the method to get user in login.service.ts:

getCurrentUser() {
    return this._storage.get<User>(this.USER_KEY);
  }// End getCurrentUser ()

This is my app.module.ts:

import { SharedModule } from './shared/SharedModule';
import 'hammerjs';

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { MaterialModule } from '@angular/material';
import { FlexLayoutModule } from "@angular/flex-layout/flexbox";

import { LocalStorageModule } from 'angular-2-local-storage';
import { AppComponent } from './app.component';
import { LoginComponent } from './modules/login/login.component';
import { Angular2RoutingModule } from './app.routing';
import { KeysPipe } from './pipes/keys.pipe';
import { AdminComponent } from './modules/admin/admin.component';
import { AdminHomeComponent } from './modules/admin/admin-home/admin-home.component';
import { ProfileComponent } from './modules/profile/profile.component';

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    ],
  imports: [
    SharedModule,
    BrowserModule,
    FormsModule,
    HttpModule,
    Angular2RoutingModule,
    MaterialModule.forRoot(),
    FlexLayoutModule,
    LocalStorageModule.withConfig({
      prefix: 'rsm',
      storageType: 'localStorage'
    })
  ],
  providers: [],
  bootstrap: [AppComponent],
  exports: []
})
export class AppModule { }

This is my profile.module.ts:

import { MaterialModule } from '@angular/material';
import { profileRouting } from './profile.routing';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { ProfileComponent } from './profile.component';
import { ProfileHomeComponent } from './profile-home/profile-home.component';
import { ProfileSecurityComponent } from './profile-security/profile-security.component';
import { ProfileSettingsComponent} from './profile-settings/profile-settings.component';
import { FlexLayoutModule} from '@angular/flex-layout/flexbox';

@NgModule({
    declarations: [
        ProfileComponent,
        ProfileHomeComponent,
        ProfileSecurityComponent,
        ProfileSettingsComponent, 
    ],

    imports: [profileRouting, CommonModule, MaterialModule.forRoot(),FlexLayoutModule],
    providers: [],
})
export class ProfileModule {

}

How i use that method in loginservice inside the component in profilemodule?

Upvotes: 2

Views: 1952

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 150

Start by separating login from application module. Create login module.

Now, since you want to use something from entirely different module. You will need to import that module.

So, you should

  • import login module in profile module
  • import login service in profile component

and that should do it.

It's like importing HttpModule and then importing Http service before using them.

Upvotes: 1

Related Questions