2017-02-17 31 views
3

私はLoginServiceからプロファイルモジュールにユーザーを取得しようとしています。私のプロファイルモジュールには3つのコンポーネントがあります。 Loginserviceは、AppModule内の別のコンポーネントにあります。angular2を使用して別のモジュールからサービスをインポートするにはどうすればよいですか?

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 { } 

これは私のprofile.module.tsさ:

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

これは私のapp.module.tsです: これはlogin.service.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 { 

} 

私はどのようにloginserviceのコンポーネント内のprofilemoduleでそのメソッドを使用しますか?

+0

AppModuleのプロバイダとしてログインサービスを追加しようとしましたか? –

+0

https://angular.io/docs/ts/latest/guide/dependency-injection.html#!#injector-providers –

+0

はい。ただし、appModuleのプロバイダとしてログインサービスを追加すると、このError.Errorが返されます。 :タイプLoginComponentは、AppModuleとProfileModuleの2つのモジュールの宣言の一部です! LoginComponentをAppModuleとProfileModuleをインポートする上位モジュールに移動することを検討してください。 LoginComponentをエクスポートして含む新しいNgModuleを作成し、そのNgModuleをAppModuleおよびProfileModuleにインポートすることもできます。 – SujaniWickramasinghe

答えて

1

ログインをアプリケーションモジュールから分離することから始めます。ログインモジュールを作成します。

これで、まったく別のモジュールから何かを使いたいからです。そのモジュールをインポートする必要があります。

だから、あなたは

  • 輸入ログインプロファイルコンポーネント

でプロファイルモジュール内のモジュール

  • 輸入ログインサービスと、それはそれを行う必要がありますする必要があります。

    HttpModuleをインポートしてからHttpサービスをインポートするのと同じです。

  • 関連する問題