8.1.与服务器通讯

Http通讯

了解angular的Http服务 发送http请求 处理http响应

url method header

httpModule

dataSource:Observable<any>;

products:Array<any>=[];

construction(private http:Http){
let myHeaders:Headers=new Headers();
myHeaders:append("Authorization","Basic 123456")
    this.dataSource=this.http.get('/products',{headers:myHeaders})
    .map((res)=>res.json());
}

this.dataSource.subscribe(
(data)=>{
    this.products=res.json
})

proxy.conf.json

{
    "/api":{
        "target":"http://localhost:8000"
    }
}

package.json

"start":"ng serve --proxy-config proxy.conf.json"

使用 | async 管道 可以代替订阅

Last updated

Was this helpful?