问题是,我有SharedModule
和ProtectedModule
,我想在module
s中使用component
和SharedModule
module
和component
的{{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
答案 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 { }