31封装一个网络请求的服务

Posted shanlu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了31封装一个网络请求的服务相关的知识,希望对你有一定的参考价值。

①创建一个服务类

  @Injectable()

  在服务类中定义方法、数据

  sendRequest ( myUrl : string ) {

    return this.http.get( myUrl ).map(  (response : Response) => response.json()  )

  }

②给服务指定提供商

  providers : [ MyHttpService ]  模块、组件限定作用范围

③调用服务

  import { }

  this.myHttpService.sendRequset( ‘ 请求的地址 ‘ ).subscribe ( (result : any) => {  console.log (reuslt)})

以上是关于31封装一个网络请求的服务的主要内容,如果未能解决你的问题,请参考以下文章