3
我想從我的LoginService到配置文件模塊的用戶。我的配置文件模塊有3個組件。 Loginservice位於AppModule內的另一個組件中。 這是讓用戶在login.service.ts方法:如何使用angular2從另一個模塊導入服務?
getCurrentUser() {
return this._storage.get<User>(this.USER_KEY);
}// End getCurrentUser()
這是我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 { }
這是我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 {
}
我如何在profilemodule中的組件內的loginservice中使用該方法?
您是否嘗試在AppModule中將登錄服務作爲提供者添加? –
https://angular.io/docs/ts/latest/guide/dependency-injection.html#!#injector-providers –
是的。但是當我在appModule中添加登錄服務作爲提供者時,它給出了這個Error.Error :類型LoginComponent是2個模塊聲明的一部分:AppModule和ProfileModule!請考慮將LoginComponent移動到導入AppModule和ProfileModule的較高模塊。您還可以創建一個新的NgModule,它導出幷包含LoginComponent,然後將該NgModule導入到AppModule和ProfileModule中。 – SujaniWickramasinghe