【问题标题】:Angular2 QuickStart : Http Nor foundAngular 2 快速入门:找不到 Http
【发布时间】:2017-09-29 09:02:27
【问题描述】:

我一直在安装 Angular2 快速入门 (https://angular.io/guide/quickstart)。

我有以下模块:

  • 应用/
    • 组件/
      • 模块 1/
        • module1.component.ts
        • module1.html
    • 服务/
      • request.service.ts
    • app.component.ts
    • app.module.ts
    • main.ts

请求服务正在从 @angular/Http 导入 Http。 在 app.module.ts 中导入 Request 服务。

使用命令 npm start 启动应用程序时,"/@angular/Http" 出现 404 错误。我检查了 @angular/ 目录并加载了 http 模块,(依赖行也在 package.json 文件中,我知道这是以前版本的快速入门中的问题)

请求服务不会抛出错误:

import { Injectable } from '@angular/core';
import { Http } from '@angular/Http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';

@Injectable()

export class RequestService {

	constructor(private http:Http) {}

	sendRequest(type:string , url:string, parameters:Object) : void {
		// some code here
	}

}

app.module.ts 看起来很简单

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';

/* SERVICES */

import { RequestService } from './services/request.service';

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ],
  providers: [ RequestService ]
})
export class AppModule { }

我真的不知道为什么 Http 模块不会加载,因为源代码在我的 @angular 目录中可用。我错过了什么吗?我应该在其他地方声明吗?

非常感谢!

【问题讨论】:

    标签: angular angular-http


    【解决方案1】:

    您的app.module 中缺少HttpModule

    import { HttpModule } from '@angular/http';
    
    @NgModule({
      imports: [ BrowserModule, HttpModule ], // add HttpModule
      declarations: [ AppComponent ],
      bootstrap: [ AppComponent ],
      providers: [ RequestService ]
    })
    

    【讨论】:

    • 这个答案对您有帮助吗? :)
    猜你喜欢
    • 2017-05-23
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2017-02-23
    • 2016-07-02
    • 1970-01-01
    • 2017-05-02
    相关资源
    最近更新 更多