启用 Http 服务

  • open the root AppModule,
  • import the HttpClientModule symbol from @angular/common/http,
  • add it to the @NgModule.imports array.
  1. // app.module.ts:
  2. import {NgModule} from '@angular/core';
  3. import {BrowserModule} from '@angular/platform-browser';
  4. // Import HttpClientModule from @angular/common/http
  5. import {HttpClientModule} from '@angular/common/http';
  6. @NgModule({
  7. imports: [
  8. BrowserModule,
  9. // Include it under 'imports' in your application module
  10. // after BrowserModule.
  11. HttpClientModule,
  12. ],
  13. })
  14. export class MyAppModule {}

发起一个 get 请求

  1. @Component(...)
  2. export class MyComponent implements OnInit {
  3. results: string[];
  4. // Inject HttpClient into your component or service.
  5. constructor(private http: HttpClient) {}
  6. ngOnInit(): void {
  7. // Make the HTTP request:
  8. this.http.get('/api/items').subscribe(data => {
  9. // Read the result field from the JSON response.
  10. this.results = data['results'];
  11. });
  12. }
  13. }

Reading the full response

  1. this.http
  2. .get('https://jsonplaceholder.typicode.com/posts/1', {observe: 'response'})
  3. .subscribe(res => {
  4. console.log(res)
  5. })

错误处理

  1. http
  2. .get('/api/items')
  3. .subscribe(
  4. // Successful responses call the first callback.
  5. data => {...},
  6. // Errors will call this callback instead:
  7. err => {
  8. console.log('Something went wrong!');
  9. }
  10. );

ng-http的更多相关文章

  1. 【码在江湖】前端少侠的json故事(中)ng的json

    ng的json 正所谓"人在江湖,身不由己",在开发之路上前端少侠dk遇到过种种困难,尤其在与后端进行数据对接的时候,不得不逼迫自己以极快的速度去学习和掌握一些奇招怪式,正当他以为 ...

  2. 不知道张(zhāng)雱(pāng)是谁?你out了!

    张(zhāng)雱(pāng)是谁?也许你已经听说过了,也许你还没听说过呢,不过你一定听说过老刘——刘强东,没错,这二人是有关系的,什么关系,京东是老刘的,而张雱呢?张雱是京东旗下52家关联公司法人代 ...

  3. Flume NG Getting Started(Flume NG 新手入门指南)

    Flume NG Getting Started(Flume NG 新手入门指南)翻译 新手入门 Flume NG是什么? 有什么改变? 获得Flume NG 从源码构建 配置 flume-ng全局选 ...

  4. matlab基础教程——根据Andrew Ng的machine learning整理

    matlab基础教程--根据Andrew Ng的machine learning整理 基本运算 算数运算 逻辑运算 格式化输出 小数位全局修改 向量和矩阵运算 矩阵操作 申明一个矩阵或向量 快速建立一 ...

  5. 汇编语言标志位 含义 NV UP EI NG NZ AC PE CY

    缩写原意: Overflow of = OV NV [No Overflow] Direction df = DN (decrement) UP (increment) Interrupt if = ...

  6. 走进AngularJs(二) ng模板中常用指令的使用方式

    通过使用模板,我们可以把model和controller中的数据组装起来呈现给浏览器,还可以通过数据绑定,实时更新视图,让我们的页面变成动态的.ng的模板真是让我爱不释手.学习ng道路还很漫长,从模板 ...

  7. 第一次部署Struts2时出现错误java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.class

    报如下错误 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) at org. ...

  8. 汇编语言标记寄存器标记位_NV UP EI NG NZ AC PE CY

    在8086CPU中,有一种标记寄存器,长度为16bit: 其中存储的信息被称为程序状态字(Program Status Word,PSW),以下将该寄存器简称为flag. 功能:1)用来存储相关指令的 ...

  9. 高可用Hadoop平台-Flume NG实战图解篇

    1.概述 今天补充一篇关于Flume的博客,前面在讲解高可用的Hadoop平台的时候遗漏了这篇,本篇博客为大家讲述以下内容: Flume NG简述 单点Flume NG搭建.运行 高可用Flume N ...

  10. Ngnice-国内ng学习网站

    今天给angular新手介绍一个国内开源的ng学习网站http://www.ngnice.com/这是由一批ng爱好者在雪狼大叔的带领下共同开发完成,致力于帮助更多的ng新人,他们分别是: ckken ...

随机推荐

  1. LeetCode 218. The Skyline Problem 天际线问题(C++/Java)

    题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...

  2. ARTS Week 7

    Dec 9, 2019 ~ Dec 15, 2019 Algorithm Problem 38.Count And Say 外观数列 题目链接 题目描述: 外观数列 是一个整数序列,从数字 1 开始, ...

  3. 关于JAVA中源码级注解的编写及使用

    一.注解简介: 1.1.什么是"注解": ​ 在我们编写代码时,一定看到过这样的代码: class Student { private String name; @Override ...

  4. 来看看你对Python变量理解到位了没有

    变量是编程的基础概念,Python 的变量也看似很简单,但是如果理解不当,生搬硬套,可能会遇到一些麻烦. 下面用 10 个代码示例展示 Python 的 变量 本质. 以下内容有对应的 视频 手把手详 ...

  5. 20200120--python学习第12天

    今日内容 函数中高级(闭包/高价函数) 内置函数 内置模块(.py文件) 内容回顾 函数基础概念 a.函数基本结构 def func(arg): return arg v1 = func(123) b ...

  6. 20191223-python学习第三天

    1.运算符补充 (1)in 与 not  in 学习 (2)优先级 >小于 ,<小于,计算运算关系优先级 > not > and > or 2.charm自动生成文件头部 ...

  7. RFC笔记,IPv6 Node Requirements

    Request for Comments: 6434,IPv6 Node Requirements 路由器节点必须能够生成链路本地地址 5.9.2. IPv6 Stateless Address Au ...

  8. php 安装 pdo_mysql

    首先要安装 mysql客户端 然后再安装php mysql 扩展 1.安装 mysql客户端 和 mysql开发包 使用yum安装mysql client 到mysql官网下载 yum文件  http ...

  9. Day2前端学习之路——HTML基本知识

    课程目标: 通过制作自己的简历,更加清楚地了解HTML是什么,HTML5是什么.学习基本的HTML标签,理解HTML语义化概念 任务一:回答问题 1.HTML是什么,HTML5是什么? HTML是一种 ...

  10. linux中的挂载命令

    一.查询与自动挂载 查询系统中已经挂载的设备,-l会显示卷标名称 mount [-l] oot@izm5e2q95pbpe1hh0kkwoiz tmp]# mount sysfs on /sys ty ...