发表于: 2019-03-25 22:05:45

1 638


今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin) 

ng6 http发送请求


在根组件 app.module.ts 中引入 HttpClientModule 模块。

// app.module.ts

import {HttpClientModule} from "@angular/common/http";  //引入HttpClientModule 模块

imports: [

  BrowserModule, 

  AppRoutingModule, 

  HttpClientModule  //声明HTTP模块

],


在组件中使用HTTP模块向远程服务器请求数据

1.引入HTTP模块

// list.components.ts

import { HttpClient } from "@angular/common/http"  //这里是HttpClient
2.在组件的构造函数中实例化 HttpClient

constructor(private http:HttpClient){}

3.创建用来接收数据的变量

public anyList:any

4.通过HTTP的get方法请求数据

ngOnInit() {  //这个是初始化  

this.http.get("https://api.example.com/api/list")     

.subscribe(res=>{ this.anyList = res }) }

// get方法中接收的是一个接口文件的地址,它会接收接口传递过来的数据,并默认处理为json数据。

// subscribe方法是对get接收的数据进行处理。参数 res 就是接收过来的数据对象。然后把 res 对象赋值给anyList变量。


//post

this.http.post('/a/login',null,{params:this.user}).subscribe(res => {
if(res.code === 0){
console.log(res)
} else if(res.code === -5004 || res.code === -5003){
this.user.pHide = res.message;
}
})

明天计划的事情:(一定要写非常细致的内容) 

继续任务
遇到的问题:(遇到什么困难,怎么解决的) 
收获:(通过今天的学习,学到了什么知识)

如上



返回列表 返回列表
评论

    分享到