Angular 4:在Feature模块中使用在SharedModule中导入的模块和组件

时间:2017-09-21 05:22:32

标签: angular

问题是,我有SharedModuleProtectedModule,我想在module s中使用componentSharedModule modulecomponent的{​​{1}};

shared.module.ts:

ProtectedModule

protected.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpModule, Http } from '@angular/http';
import { TranslateModule } from '@ngx-translate/core';

import { ComponentsModule } from './components/components.module';
import { DirectivesModule } from './directives/directives.module';
import { PipesModule } from './pipes/pipes.module';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    HttpModule,
    Http,
    ComponentsModule,
    DirectivesModule,
    PipesModule
  ],
  exports: [
    CommonModule,
    FormsModule,
    HttpModule,
    Http,
    TranslateModule,
    ComponentsModule,
    DirectivesModule,
    PipesModule
  ],
  declarations: []
})
export class SharedModule { }

我希望import { NgModule } from '@angular/core'; import { SharedModule } from '../shared/shared.module'; import { ProtectedRoutingModule } from './protected-routing.module'; import { ProtectedComponent } from './protected.component'; import { LoginComponent } from './login/login.component'; import { LoginService } from './login/login.service'; @NgModule({ imports: [ SharedModule, ProtectedRoutingModule ], declarations: [ ProtectedComponent, LoginComponent ], exports: [ SharedModule ], providers: [ LoginService ] }) export class ProtectedModule { } 使用login.service.ts导入导入HttpModule SharedModule导入的ProtectedModule导入LoginService。< / p>

login.service.t S:

ProtectedModule

当我在import { Injectable } from '@angular/core'; @Injectable() export class LoginService { private loginUrl = 'http://localhost/kpittu/index.php/user/authorization'; constructor(private http: Http) { } login(): void { } } 中使用HttpModule时出现错误:

  

“找不到名字Http”

编辑:

Angular版本:

login.service.ts

1 个答案:

答案 0 :(得分:1)

login.service.ts中缺少导入,请使用下面提到的内容:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';

@Injectable()
export class LoginService {

  private loginUrl = 'http://localhost/kpittu/index.php/user/authorization';

  constructor(private http: Http) { }

  login(): void {

  }

}

修改

说明: 您已导入&#34; HttpModule&#34;它允许您使用该模块的功能。但是你需要导入&#34; Http&#34;在相应的文件中。

正确版sharedModule,您不需要在此处导入Http,除非您在此处使用它。

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpModule} from '@angular/http';
import { TranslateModule } from '@ngx-translate/core';

import { ComponentsModule } from './components/components.module';
import { DirectivesModule } from './directives/directives.module';
import { PipesModule } from './pipes/pipes.module';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    HttpModule,
    ComponentsModule,
    DirectivesModule,
    PipesModule
  ],
  exports: [
    CommonModule,
    FormsModule,
    HttpModule,
    TranslateModule,
    ComponentsModule,
    DirectivesModule,
    PipesModule
  ],
  declarations: []
})
export class SharedModule { }